简体   繁体   English

样式输入材料角度

[英]Style input material angular

On the angular material and the input component (matInput) how can I modify the style of all inputs in typescript? 在角形材料和输入组件(matInput)上,如何修改打字稿中所有输入的样式?

I'm trying this way, but it only modifies the first element. 我正在尝试这种方式,但它只会修改第一个元素。

ngAfterViewInit () {
    const matlabel = this.elRef.nativeElement.querySelector ('.mat-form-field-label');
    matlabel.style.color = 'white';
  }

===================== =====================

<form [formGroup]="form" (ngSubmit)="login()">
      <table cellspacing="0" class="tb-login">
        <tr>
          <td>
            <mat-form-field class="form-login">
              <input matInput type="text" placeholder="Login" formControlName="login"  class="input-login"/>
            </mat-form-field>
          </td>
        </tr>
        <tr>
          <td>
            <mat-form-field  class="form-login">
              <input matInput type="password" placeholder="Password" formControlName="password"   class="input-login"/>
            </mat-form-field>
          </td>
        </tr>
        <tr>
          <td>
            <button mat-raised-button type="submit" class="btn_login">Login</button>
          </td>
        </tr>
      </table>
    </form>

Angular v7 Material Angular v7 Angular v7材质Angular v7

I will be grateful for suggestions. 我将为您提供建议。

Use Renderer2 along with ViewChild to do this. 结合使用Renderer2ViewChild可以做到这一点。

Renderer2 gives Platform Agnostic APIs to modify the DOM and should be used as opposed to the native document APIs. Renderer2提供了平台不可知API来修改DOM,并且应与本机document API相对使用。

You can inject Renderer2 as a dependency in your Component and then call the setStyle method on it passing the element you want to set the style on, name of the style( 'background' ) and the value of it( red in my eg). 您可以将Renderer2作为依赖项注入到Component中,然后在其上调用setStyle方法,以传递要设置样式的元素,样式名称( 'background' )和其值(例如在我的示例中为red )。

Here, give this a try: 在这里,尝试一下:

import { Component, ViewChild, Renderer2 } from '@angular/core';
import { MatInput } from '@angular/material/input';

@Component({...})
export class InputOverviewExample {

  @ViewChild('food') matInput: MatInput;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
    this.renderer.setStyle(this.matInput.nativeElement, 'background', 'red');
  }

}

And in your Template, create template variable(s) like I've done on the input tag with the name food . 然后在您的模板中,创建模板变量,就像我在input标签上完成的那样,将其命名为food This is what I'm using in @ViewChild('food') : 这就是我在@ViewChild('food')

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input #food matInput placeholder="Favorite food" value="Sushi">
  </mat-form-field>

  <mat-form-field class="example-full-width">
    <textarea matInput placeholder="Leave a comment"></textarea>
  </mat-form-field>
</form>

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM