繁体   English   中英

Angular 2 / Ionic 2-输入框聚焦功能不存在

[英]Angular 2 / Ionic 2 - input box focus function doesn't exist

在我的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);
        });
    });
}

因此,在View初始化后,将光标聚焦在模式上的输入框上。 但是,当我的代码在View初始化上运行时,由于该函数不存在而失败:

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

在控制台上,它表示焦点是一个EventEmitter,但是我不确定如何使用它来实现我想要的功能。

任何帮助,将不胜感激。

孟加拉

您需要使用setFocus()函数。

this.inputBox.setFocus()

来源讨论

同样根据文档focusion-input的输出事件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM