相关疑难解决方法(0)

如何在按钮点击上关注ion-searchbar

我试图专注ion-searchbar于按钮点击,但它无法正常工作.这是我的代码

打字稿

@ViewChild('search') search:ElementRef;

focusButton(){
       console.log(this.search);   //Searchbar {_config: Config, _elementRef: ElementRef, _renderer: RendererAdapter, _componentName: "searchbar", _mode: "md", …}
       console.log(this.search.nativeElement); //  null
       this.search.nativeElement.focus();      //  Cannot read property 'focus' of undefined
        this.search.nativeElement.setFocus();      // Cannot read property 'setFocus' of undefined

}
Run Code Online (Sandbox Code Playgroud)

HTML

<ion-searchbar #search (ionCancel)="cancelSearch($event)" [showCancelButton]="true" [(ngModel)]="artists"></ion-searchbar>
        <ion-buttons end>
            <button [hidden]="showSearch" (click)="(showSearch = !showSearch) && focusButton()" ion-button icon-only>
              <ion-icon  name="search"></ion-icon>
            </button>
        </ion-buttons>
Run Code Online (Sandbox Code Playgroud)

控制台输出位于带代码的注释中.

typescript ionic3 angular

3
推荐指数
3
解决办法
4450
查看次数

标签 统计

angular ×1

ionic3 ×1

typescript ×1