Set focus on an input with Ionic 2

后端 未结 10 2043
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 00:29

SOLVED :

import { Component, ViewChild} from \'@angular/core\';
import { Keyboard } from \'ionic-native\';


@Component({
  templateUrl: \         


        
10条回答
  •  一生所求
    2020-12-14 01:02

    In my case, for some reason, ionViewLoaded() was not getting triggered. Tried ionViewDidLoad() and set the timer to 200 and it worked.

    150 proved too early for me. Complete Solution:

    import { Component, ViewChild } from '@angular/core';//No need to import Input
    
    export class HomePage {
    
      @ViewChild('inputToFocus') inputToFocus;
      constructor(public navCtrl: NavController) {}
    
      ionViewDidLoad()
      {
        setTimeout(() => {
          this.inputToFocus.setFocus();
        },200)
      }  
    }
    

    And on the input tag:

    
    

提交回复
热议问题