私のアプリケーションでは、入力フィールドをブール値のコンポーネントプロパティで表示/非表示するボタンを配置しようとしました。ボタンに入力が表示されている場合は、入力にフォーカスを設定する必要があります。しかし、それは動作していないようです。 *ngIf
を削除すると、フォーカス指示が正常に動作します。フォーカスディレクティブで* ngIfを使用するAngular2?
私はplunkerを作成しました。私の "問題"を説明するのは難しいです。成分中
HTML
<input *ngIf="filterShow.options"
[focus]="filterFocus.options"
[(ngModel)]="filter.options">
<button type="button"
(click)="setShowFilter('options')">
focus
</button>
setShowFilter()関数
private setShowFilter(filter: string) {
this.filterShow[filter] = !this.filterShow[filter];
/* reset filter */
this.filter[filter] = "";
this.filterFocus[filter].emit(true);
}
focus.directive.ts
@Directive({
selector: '[focus]'
})
export class FocusDirective implements OnInit {
@Input('focus') focusEvent: EventEmitter<boolean>;
constructor(private elementRef : ElementRef,
private renderer : Renderer ) { }
ngOnInit() {
this.focusEvent.subscribe(event => {
this.renderer
.invokeElementMethod(this.elementRef.nativeElement, 'focus', []);
});
}
}
イベントエミッタが間違っています。彼らは子供 - >親のコミュニケーションのためであり、親 - >子供のためのものではありません。私が投稿したものは私のために働いています:https://plnkr.co/edit/YtaRtA0e5L6ewxq7uCH9?p=preview – adharris
'Renderer'と' .invokeElementMethod() 'メソッドは、この時点で廃止されました。 'this.elementRef.nativeElement.focus()'を直接呼び出すことは安全です。 –