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); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- BPTMQEditor文档编辑器插件开发(基于bootstrap[它的一些样式
- docker之什么是docker?
- 从无到有,WebService Apache Axis2初步实践
- shell – 如何编写像Top这样的交互式程序?
- 如何将参数转发到bash脚本中的其他命令?
- 如何正确地将document.addEventListener添加到Angular2?
- AngularJS 学习笔记 (三) 基本概念和用法 之 路由、模块、
- angular 2:TypeError:this.form.updateValueAndValidity不
- ERROR in AppModule is not an NgModule
- 为社区制作Scala项目的推荐方法是什么?