【发布时间】:2018-10-05 00:32:17
【问题描述】:
我正在使用 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();
}
搜索框未设置为焦点。
我该怎么做?
【问题讨论】:
标签: angular angular5 angular-forms