12
<input>要素にフォーカスを設定します
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"); …