【问题标题】:Angular 8 focus on material input from methodAngular 8 专注于方法的材料输入
【发布时间】:2019-12-10 19:48:45
【问题描述】:

在我的应用程序中,我使用的是 Angular 8、反应形式和材料设计者。

在特定情况下,我需要将重点放在方法中的一个输入上。

当我研究时,我发现有一种方法可以使用“ElementRef”来做到这一点,有几个例子,这就是其中之一: Angular 6 Reactive Forms : How to set focus on first invalid input

在这个(或任何)ElementRef 上我找不到 focus() 方法(在 nativeElement.focus() 中也没有)。

有没有办法直接在formeControle上,或任何其他方式。

【问题讨论】:

    标签: angular angular-material angular-reactive-forms


    【解决方案1】:

    您无法通过formControl 执行此操作,但是您可以通过在模板中使用模板变量或在组件中使用@ViewChild 来执行此操作。

    <mat-form-field>
      <input matInput #myInput>
    </mat-form-field>
    <-- You can focus from template this way -->
    <button (click)="myInput.focus()">Focus</button>
    

    将焦点设置在组件上:

    export class MyComponent {
    
      @ViewChild('myInput') input: MatInput;
    
      // You can focus from component this way
      focusMyInputFromComponent() {
         this.input.focus();
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2019-11-21
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      相关资源
      最近更新 更多