I want a function to trigger within my parent component when my child component emits an event. The child component is passed into the parent through ng-content. I simplified the code of what I'm trying to do but I can't seem to have the child emit to the parent. I want to pass the child with ng-content because the child component can be a variety of components and not 1 single specific one
Example:
main.ts
<parent-component>
<child-component></child-component>
</parent-component>
parent.component.ts
<div>
{{ this.value }}
<ng-content (updated)="updateParentValue()"></ng-content> <-- this doesn't work
</div>
updateParentValue(value) {
this.value = value;
}
child.component.ts
<div>
<span (click)="updateStuff()">update</span>
</div>
@Output() updated = new EventEmitter();
updateStuff() {
// Stuff
this.updated.emit(this.value);
}
Thanks Krim, solved it with that link
main.ts
<parent-component>
<child-component></child-component>
</parent-component>
parent.component.ts
<div>
{{ this.value }}
<ng-content></ng-content>
</div>
@ContentChildren(ChildComponent) childComponents: QueryList<ChildComponent>;
ngAfterViewInit() {
this.childComponents.forEach((childComponent: ChildComponent) => {
childComponent.updated.subscribe((item) => this.updateParentValue(item));
});
}
updateParentValue(value) {
this.value = value;
}
child.component.ts
<div>
<span (click)="updateStuff()">update</span>
</div>
@Output() updated = new EventEmitter();
updateStuff() {
// Stuff
this.updated.emit(this.value);
}