我刚接触Angular ,正在try 使用它将焦点设置在id为"input1"的输入上.我使用以下代码:
@ViewChild('input1') inputEl: ElementRef;
然后在组件的后面:
this.inputEl.nativeElement.focus();
但它不起作用.我做错了什么?任何帮助都将不胜感激.
我刚接触Angular ,正在try 使用它将焦点设置在id为"input1"的输入上.我使用以下代码:
@ViewChild('input1') inputEl: ElementRef;
然后在组件的后面:
this.inputEl.nativeElement.focus();
但它不起作用.我做错了什么?任何帮助都将不胜感激.
@Z.Bagley提到的问题中的一个答案给了我答案.我必须将Renderer2从@angular/core导入到我的组件中.然后:
const element = this.renderer.selectRootElement('#input1');
setTimeout(() => element.focus(), 0);
"谢谢您,"布莱斯先生提出了这个解决方案!