[英]Angular 8 focus on material input from method
在我的應用程序中,我使用的是 Angular 8、反應形式和材料設計者。
在特定情況下,我需要將重點放在我從方法中提交的輸入之一上。
在我研究時,我發現有一種方法可以使用“ElementRef”來做到這一點,有幾個例子,這是其中一個: Angular 6 Reactive Forms : How to set focus on first invalid input
在這個(或任何)ElementRef 上我找不到 focus() 方法(在 nativeElement.focus() 中也沒有)。
有沒有辦法直接在formeControle上或任何其他方式上做到這一點。
您不能從formControl
執行此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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.