2017-03-03 66 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