How to call parent component's function when child component observed input changes?
The below is HTML structure.
# app.comopnent.html
<form>
<textbox>
<input type="text">
</textbox>
</form>
# textbox.component.html
<div class="textbox-wrapper">
<ng-content>
</div>
Restrictions are like following.
- TextboxComponent has
ng-content
and need to projectinput
element to it. - Emit an event in TextboxComponent when
input
element is inputted something. - Don't wanna make
input
element to have more attributes, e.g.<input type="text" (input)="event()">
.
I was writing code, but cannot find a solution...
# 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...
});
}
}