Angular 2 / Ionic 2 - 输入框焦点功能不存在
Angular 2 / Ionic 2 - input box focus function doesn't exist
在我的 Ionic 2 应用程序中,我有一个页面,上面有一个按钮。当您按下按钮时,它会弹出一个带有输入框的模态屏幕。当 Modal 页面打开时,我想将光标聚焦在 Modal 页面上的输入框上,以便用户可以立即开始输入。
在我的模式页面中,我有以下 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);
});
});
}
因此在 View 初始化后,将光标聚焦在模态的输入框上。但是当我的代码在 View 初始化上运行时,由于函数不存在而失败:
VM415:1 Uncaught TypeError: this.inputBox.focus is not a function(…)
在控制台上它说 focus 是一个 EventEmitter 但我不确定如何使用它来实现我的目标我想要。
如有任何帮助,我们将不胜感激。
本格拉
您需要使用setFocus()
功能。
this.inputBox.setFocus()
同样根据the docs,focus
是ion-input
的输出事件。
在我的 Ionic 2 应用程序中,我有一个页面,上面有一个按钮。当您按下按钮时,它会弹出一个带有输入框的模态屏幕。当 Modal 页面打开时,我想将光标聚焦在 Modal 页面上的输入框上,以便用户可以立即开始输入。
在我的模式页面中,我有以下 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);
});
});
}
因此在 View 初始化后,将光标聚焦在模态的输入框上。但是当我的代码在 View 初始化上运行时,由于函数不存在而失败:
VM415:1 Uncaught TypeError: this.inputBox.focus is not a function(…)
在控制台上它说 focus 是一个 EventEmitter 但我不确定如何使用它来实现我的目标我想要。
如有任何帮助,我们将不胜感激。
本格拉
您需要使用setFocus()
功能。
this.inputBox.setFocus()
同样根据the docs,focus
是ion-input
的输出事件。