我试图专注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)
控制台输出位于带代码的注释中.