简体   繁体   中英

How to set focus on Input Angular2 Form

I have an Input in angular2/Ionic2 form

<input #input1 autofocus type="text" [ngModel]='searchString' name='searchText' required minlength="3" (ngModelChange)="onInputChange($event)">

I want to set focus on it when page/route change.

nativeElement.focus() is not working as @ViewChild(#input1) gives angular2 element not native html element.

what is 'autofocus' for? is not necessary

 @ViewChild ('input1') el: ElementRef; // the # is used in html only .... async ngOnInit() { setTimeout(() => { this.el.nativeElement.focus(); // with ElementRef should work, but if you encounter any problem just use 'any' type }, 1000); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM