加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – 如何在按钮单击时聚焦ion-searchbar

发布时间:2020-12-17 17:03:23 所属栏目:安全 来源:网络整理
导读:我试图在按钮点击上关注ion-searchbar,但它无法正常工作. 这是我的代码 打字稿 @ViewChild('search') search:ElementRef;focusButton(){ console.log(this.search); //Searchbar {_config: Config,_elementRef: ElementRef,_renderer: RendererAdapter,_comp
我试图在按钮点击上关注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>

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

解决方法

希望你可以使用下面的代码来完成它.只需使用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);
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读