在Angular对话框组件中添加一个局部变量,并在ngAfterViewInit钩子函数中为其添加焦点,这样在对话框打开时就可以移动键盘焦点。示例代码如下:
HTML模板:
组件类:
import { Component, ViewChild, AfterViewInit } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
})
export class MyComponent implements AfterViewInit {
@ViewChild('dialogBox', { static: true }) dialogBox;
ngAfterViewInit() {
this.dialogBox.nativeElement.focus();
}
}
这样的话,我们在弹出对话框后,会自动将焦点放在对话框中,这样可以方便用户使用键盘。
上一篇:Angular对话框大小
下一篇:Angular对话框的行为异常