2017-03-03 32 views
0

在我的Ionic 2應用程序中,我有一個頁面上有一個按鈕。當你按下按鈕時,它會彈出一個帶有輸入框的Modal屏幕。當Modal頁面打開時,我想將光標聚焦在Modal頁面的輸入框上,以便用戶可以立即開始輸入。Angular 2/Ionic 2 - 輸入框對焦功能不存在

在我的模態頁,我有以下HTML

<ion-item> 
    <ion-input #search type="text" placeholder="Type an area e.g. Bedroom" [value]="searchValue" [formControl]="inputSearchControl"></ion-input> 
</ion-item> 

在我的代碼,我有以下幾點:

@ViewChild("search") inputBox; 

    ngAfterViewInit() { 

    this.inputBox.nativeElement.focus(); 

    this.inputSearchControl.valueChanges.debounceTime(500).subscribe((callbackText) => { 
     this.selectedArray = this.originalArrayBeforeAnyFiltering.filter((item) => { 
      return (item.toLowerCase().indexOf(callbackText.toLowerCase()) > -1); 
     }); 
    }); 
} 

所以查看已初始化後,重點將光標定位到在模態上的輸入框。但是,當我的代碼在查看正開始運行時,它失敗,因爲該功能不存在:

VM415:1 Uncaught TypeError: this.inputBox.focus is not a function(…)

在控制檯它說,焦點EventEmitter,但我不知道如何使用這個來實現我想要的。

任何幫助,將不勝感激。

bengrah

回答

1

您需要使用setFocus()函數。

this.inputBox.setFocus() 

this discussion

此外,根據the docsfocusion-input一個輸出事件。

+0

這正是我需要的 - 這對我很有用。謝謝你的幫助。 – bengrah

+1

@suraj這件事不適合我。在我的 devanshsadhotra