子要素の入力が変化したときに親要素の関数を呼び出す方法は?入力要素の変化を観測する方法
以下はHTML構造です。
# app.comopnent.html
<form>
<textbox>
<input type="text">
</textbox>
</form>
# textbox.component.html
<div class="textbox-wrapper">
<ng-content>
</div>
制限事項は次のとおりです。
- TextboxComponentは
ng-content
を持っており、それにinput
要素を投影する必要があります。 input
要素が入力されたときに、TextboxComponentでイベントを送出します。input
要素にさらに多くの属性を追加したくないとします。<input type="text" (input)="event()">
。
私は、コードを書いていたが、解決策を見つけることができません...
# input.directive.ts
@Directive({ selector: 'input', ... })
export class InputDirective {
ngOnChanges(): void {
// ngOnChanges() can observe only properties defined from @Input Decorator...
}
}
# textbox.component.ts
@Component({ selector: 'textbox', ... })
export class TextboxComponent {
@ContentChildren(InputDirective) inputs: QueryList<InputDirective>;
ngAfterContentInit(): void {
this.inputs.changes.subscribe((): void => {
// QueryList has a changes property, it can observe changes when the component add/remove.
// But cannot observe input changes...
});
}
}
は、なぜあなたは、出力を使用したくないあなたのモジュールの角CDKのオブザーバーhttps://material.angular.io/cdk/observers/api
インポートこのモジュールを使用することができますバインディングは、のように?他の開発者(またはあなたも)がTextboxComponentのマークアップに入力要素だけを追加することをどうやって確認できますか? – rrjohnson85
出力バインディングを 'input'要素に追加する必要があるので、それを使用したい場合は冗長です。しかし、代わりに 'host:{(input): 'event()'}'プロパティをInputDirectiveで使うことができます。 最大の問題は、 'ng-content'を使っているTextboxComponentがInputDirectiveが何かを入力した時を検出できることです。 –