Home > database >  Angular: Why is the variable undefined after ngAfterContentInit?
Angular: Why is the variable undefined after ngAfterContentInit?

Time:12-29

I'm trying to get access to ng content with @ContentChild. To better understand @ContentChild I tried to implement a simple code example. I also did research such as here Output

CodePudding user response:

You're almost right... One thing: the refs right place is on top the children directly.

<app-parent>
  <app-child #refChild></app-child>
</app-parent>

Then it's not more undefined:

enter image description here

For the other direction you can use Services, EventEmitter or the Host decorator like this (it's the child constructor):

constructor(@Host() parent: ParentComponent) {
 console.log("Here it is", parent)
}
  • Related