Angular2 - Focusing a textbox on component load

parag picture parag · Mar 4, 2016 · Viewed 123.4k times · Source

I am developing a component in Angular2 (Beta 8). The component has a textbox and a dropdown. I would like to set the focus in textbox as soon as component is loaded or on change event of dropdown. How would I achieve this in angular2. Following is Html for the component.

Answer

Nikhil Nambiar picture Nikhil Nambiar · Feb 3, 2017

Using simple autofocus HTML5 attribute works for 'on load' scenario

 <input autofocus placeholder="enter text" [(ngModel)]="test">

or

<button autofocus (click)="submit()">Submit</button>

http://www.w3schools.com/TAgs/att_input_autofocus.asp