Ionic v4 - how do I autofocus on a Input?

SpaceDogCS picture SpaceDogCS · Feb 27, 2019 · Viewed 9.8k times · Source

I have a view with an input <ion-input #codigobarras></ion-input>

How can I auto focus on this input?

What have I tried

@ViewChild('codigobarras') input_codigobarras: Input;

...

ionViewDidEnter() {
    this.input_codigobarras.focus(); // didn't work = temp2.focus is not a function
    this.input_codigobarras.focus.emit(); // didn't work = do nothing, just returns undefined
    this.input_codigobarras.getElementRef().nativeElement.focus() // didn't work = do nothing, just returns undefined
    this.input_codigobarras.setFocus(); // didn't work = do nothing, just returns undefined
}
<ion-input [autofocus]></ion-input> <!-- Didn't work -->

Answer

ppichier picture ppichier · Feb 27, 2019

This should work

<ion-input autofocus="true"></ion-input>