2017-08-20 69 views
2

我想重點按鈕點擊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 

} 

的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> 

控制檯輸出上面與代碼中的註釋。

回答

3

希望你可以使用下面的代碼來做到這一點。只需使用setTimeout就可以了,如下所示。

的.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> 

.TS

@ViewChild('search') search : any; 

focusButton(): void { 
    setTimeout(() => { 
     this.search.setFocus(); 
    }, 500); 
    } 
+0

隊友嗨,我得到的setFocus是不是一個功能 – user1195369

+0

你可以把一個新的問題,讓我知道? @ user1195369 – Sampath