On the ngAfterViewInit
lifecycle in angular, I'd like to focus()
and select()
the input. I've played around with it a bit and it seems like select()
only works after I set a timeout. For example, the code below works as intended (input is focused and selected).
<input #el>
@ViewChild('el') el;
ngAfterViewInit() {
setTimeout(() => {
this.el.nativeElement.focus();
this.el.nativeElement.select();
}, 0);
}
But just doing this, only focus
works.
ngAfterViewInit() {
this.el.nativeElement.focus();
this.el.nativeElement.select();
}
Can someone explain why this is?