Set focus on an input with Ionic 2

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

SOLVED :

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


@Component({
  templateUrl: \         


        
10条回答
  •  星月不相逢
    2020-12-14 01:00

    For IOS and Android its fine working for me. put focus code in ionViewWillEnter().

    import { Component, ViewChild, ElementRef } from '@angular/core';
     import { Keyboard } from '@ionic-native/keyboard';
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    export class HomePage {
     @ViewChild("Input") inputEl: ElementRef;
     constructor(public keyboard:Keyboard){}
    
     ionViewWillEnter() { 
        setTimeout(() => {           
          this.inputEl.nativeElement.focus();
          this.keyboard.show();    
        }, 800); //If its your first page then larger time required 
    }
    

    Input tag in html file

     
    

    And add this line to your config.xml to make it work on iOS :

    
    

提交回复
热议问题