I have two shared components and I want the parent to invoke a method in its child
shared component 1 (parent)
@Component({
selector: 'parent',
template: `<div>
<div #parentBody>
<ng-content select="[parentBody]"></ng-content>
</div>
<button (click)=" " >tell child to dance</button>
</div>
`,
})
export class ParentComponent {
constructor() { }
}
shared component 2 (child)
@Component({
selector: 'child',
template: `<div>
<p>I'm a child component</p>
</div>
`,
})
export class ChildComponent {
dance() {
alert('dancing');
}
}
and in app component
<parent>
<div parentBody>
<child></child>
</div>
<parent>
how can we communicate between the parent and child component in this case
@ContentChilddecorator, juste like the@ViewChild. It allows you to have a reference to your component instance and do whatever you want with it. To have a parent reference in the child, you can use the@Hostdecorator on a dependency injection.