我正在使用 ng-select
并定义了一个包含输入的自定义过滤器 header 模板。
我希望在为选择打开下拉列表时输入获得焦点,但我不知道该怎么做。
我这里有一个例子 -> https://stackblitz.com/edit/angular-playground-f57jog
我确实尝试附加到 ng-select (open)
输出并在我的输入元素上调用 focus()
,但这对我来说失败了。
这里正确的方法是什么? 谢谢
最佳答案
我会创建特殊指令并在需要的地方重用它:
@Directive({
selector: '[appAutofocus]'
})
export class AutofocusDirective implements OnInit {
constructor(private el: ElementRef) {
}
ngOnInit() {
this.el.nativeElement.focus();
}
}
html
<input appAutofocus ... />
所以它应该适用于 Chrome、Firefox 等。
关于angular - 将 ng-select 过滤器输入聚焦在打开的下拉菜单上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56259390/