How to use Angular4 to set focus by element id

后端 未结 6 1474
無奈伤痛
無奈伤痛 2020-12-14 14:11

I am new to Angular, and am trying to use it to set focus on an input with the id \"input1\". I am using the following code:



        
6条回答
  •  清歌不尽
    2020-12-14 14:57

    I also face same issue after some search I found a good solution as @GreyBeardedGeek mentioned that setTimeout is the key of this solution.He is totally correct. In your method you just need to add setTimeout and your problem will be solved.

    setTimeout(() => this.inputEl.nativeElement.focus(), 0);
    

提交回复
热议问题