【发布时间】:2023-03-07 02:00:01
【问题描述】:
在我的角度项目中,有角度材料并使用 mat-select。在我的案例中,Mat-select 是我的表单的第一个元素,在成功加载页面时设置了自动焦点,但我无法在 mat-select 上设置自动焦点。任何人都可以帮助我找到在 mat-select 中设置自动对焦的方法。
@ViewChild("name") nameField: ElementRef;
ngOninit() {
this.nameField.nativeElement.focus();
}
html
<div>
<mat-select [(ngModel)]="nameField" #name>
<mat-option *ngFor="let option of options2" [value]="option.id">
{{ option.name }}
</mat-option>
</mat-select>
</div>
【问题讨论】:
-
[focused]="true"? -
您必须为此目的使用指令。检查这个答案:*.com/a/44729559/7630248
-
请在 Stack Overflow 上包含您尝试过的代码。
-
我还是不明白你为什么选择了我的答案并放入问题中
标签: javascript angular typescript angular-material