Set focus on an input with Ionic 2

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

SOLVED :

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


@Component({
  templateUrl: \         


        
10条回答
  •  生来不讨喜
    2020-12-14 00:59

    I found this solution to also fix the problem that the keyboard is pushing the content away.

    
    
      Name
      
    
    
    

      @ViewChild(Content) content: Content;
    
      focusMyInput(inputRef) {
        const itemTop = inputRef._elementRef.nativeElement.getBoundingClientRect().top;
        const itemPositionY = this.content.scrollTop + itemTop -80;
    
        this.content.scrollTo(null, itemPositionY, 500, () => {
          inputRef.setFocus();
        });
      }
    

提交回复
热议问题