重点说明: 假如先设定了 输入框disabled又打开,然后执行获取焦点,可能会失效,这个时候可以把getFocus函数放在定时器里,1秒钟后执行,就可以了哦~
具体代码如下
html
<input #myInput id="myInputr" nz-input class="myInput" [(ngModel)]="aaa.value"
(keyup.enter)="myClick()" />
ts文件如下
import { Component, OnInit, ElementRef } from '@angular/core';
@ViewChild('myInput', { static: true }) myInput: ElementRef;
ngAfterViewInit() {
this.getFocus()
}
getFocus() {
this.myInput.nativeElement.focus()
// document.getElementById('myInputr').focus()
}