2017-11-17 8 views
0

私は、入力フィールドのフォーカスにディレクティブを使用しています。これは、最初にページに入るときにうまくいきます。しかし、このメソッドは、私が戻るか、特定のページにポップすると呼び出されません。 ngAfterViewInit()がもう呼び出されていないからだと思います。私はionViewDidEnter()/onPageDidEnter()を使用しようとしましたが、この場合フォーカスはまったく呼び出されません。一部のページで使用入力フィールドをフォーカスするためのディレクティブの使用

<ion-input type="text" [(ngModel)]="matrikel" focus> 

focuser.ts

@Directive({ 
    selector: '[focus]' // Attribute selector 
}) 

export class FocusDirective { 

    @Input() focus: any; 


    constructor(private el: ElementRef, private renderer: Renderer, private globalVariables: GlobalsVariable) { } 

    ngAfterViewInit(){ 

      const searchInput = this.el.nativeElement.querySelector('input'); 
      setTimeout(() => { 
       this.renderer.invokeElementMethod(searchInput, 'focus', []); 
      }, 650); 
} 

} 

答えて

0

あなたはionViewCanEnter()を使用する必要があります。 ionViewDidLoad()の前に電話する予定です。

HTML

<ion-input #focusInput type="text" [(ngModel)]="matrikel"> 

focuser.ts

import { ViewChild } from '@angular/core'; 

    export class FocusDirective { 
    @ViewChild('focusInput') focus: any; 

    constructor(private el: ElementRef, private renderer: Renderer, private globalVariables: GlobalsVariable) { } 

    ionViewCanEnter() { 
     setTimeout(() => { 
      this.focus.setFocus(); 
     }, 500); 
    } 
    } 
関連する問題