I have add buttons at two separate places inside my component. On click of the button, the input field shows up. I have tried multiple ways to achieve this as follows:
Used autofocus attribute on the input field - as i have two buttons on the screen, autofocus does not seem to work.
Used .focus() method -
HTML File
<button (click) = "addUser()">Add User</button>
<input *ngIf="showInputField" #userName />
TS File
@ViewChild('userName') userName:ElementRef;
addUser() { this.showInputField=true; this.userName.nativeElement.focus(); }
In second case, i got the error "cannot read the property nativeElement of undefined", which is because the HTML loads after the execution of method completes.