0

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.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
raju
  • 6,448
  • 24
  • 80
  • 163

1 Answers1

1

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);
}
Luca Taccagni
  • 1,059
  • 6
  • 23