Reputation: 39
I am trying to set focus() and select() on input text in ngAfterViewInit() in angular ,but it doesn't select the input text.But If I use it with setTimeout() it works
Code
component.html
<input [(ngModel)]="inputName" #inputtext />
component.ts-
export class Test implements AfterViewInit{
private _inputName:string;
@ViewChild("inputtext") private _inputTextbox: ElementRef;
constructor(){
this._inputName="someText";
}
public get inputName():string{
return this._inputName;
}
public ngAfterViewInit(): void {
this._inputTextbox.nativeElement.focus();
this._inputTextbox.nativeElement.select();
}
}
I was expecting that it should focus and select the input text once input textbox is visible. For above code,if I use setTimeout to focus and select ,it works.
Upvotes: 0
Views: 220