How to set focus on element with binding?

后端 未结 9 957
南方客
南方客 2020-12-09 10:23

In Angular2 how can I set binding on element focus. I don\'t want to set it with elementRef. I think in AngularJS there is ngFocus directive In Angular2 there is no such dir

9条回答
  •  -上瘾入骨i
    2020-12-09 10:28

    From @MrBlaise I took the setTimeout snippet which made the following work for me.

    
    
    
    
    import { ElementRef, ViewChild } from '@angular/core';
    
    ...
    
    @ViewChild('searchInput') private searchInput: ElementRef;
    
    ...
    
    setTimeout(() => this.searchInput.nativeElement.focus(), 0);
    

提交回复
热议问题