我正在使用Angular 5进行前端应用程序,我需要隐藏一个搜索框,但点击一个按钮,搜索框将显示并设置焦点.
我已尝试使用指令左右的stackoverflow中找到的几种方法,但没有运气.
以下是示例代码:
@Component({ selector: 'my-app',template: ` <div> <h2>Hello</h2> </div> <button (click) ="showSearch()">Show Search</button> <p></p> <form> <div > <input *ngIf="show" #search type="text" /> </div> </form> `,}) export class App implements AfterViewInit { @ViewChild('search') searchElement: ElementRef; show: false; name:string; constructor() { } showSearch(){ this.show = !this.show; this.searchElement.nativeElement.focus(); alert("focus"); } ngAfterViewInit() { this.firstNameElement.nativeElement.focus(); }
搜索框未设置为焦点.
我怎样才能做到这一点?谢谢.
像这样修改show search方法
showSearch(){ this.show = !this.show; setTimeout(()=>{ // this will make the execution after the above boolean has changed this.searchElement.nativeElement.focus(); },0); }
原文地址:https://www.jb51.cc/angularjs/140662.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。