0

I try to get in the child component reference to its parent Angular component based on components tree. What is the correct way to do this? I have found out that injector.view is actually the reference I am looking for but this API is not documented.

NOTE: Why I need this. I am mixing Angular with a canvas rendering library. The child actually will treat the parent as AbstractComponent it does not care about the concrete class of its parent. Since I am building a parallel canvas display tree on top of the Angular's components tree and the child need to add it self into the parents canvas container.

I am trying to inject into ChildComponent reference to the ParentComponent.

Example:

@Component({selector: "ParentComponent",
template: '<ChildComponent></ChildComponent>'})
export class ParentComponent extends AbstractComponent
{
      constructor(){ super()}
}


@Component({selector: "ChildComponent",
template: '<h1>Child</h1>})
export class ChildComponent extends AbstractComponent
{
      constructor(){ super()}
}
Hivaga
  • 3,738
  • 4
  • 23
  • 36
  • 2
    Could you expand a bit on the *context* - why does the child need a reference to the parent? – jonrsharpe Apr 27 '18 at 08:54
  • @jonrsharpe is right. This may be a [XY problem](http://xyproblem.info/). Please explain why you need the parent in the child. – CornelC Apr 27 '18 at 08:56
  • Well the child actually will treat the parent as AbstractComponent it does not care about the concrete class of its parent. This is needed since I am building a parallel canvas display tree and the child need to add it self into the parents canvas container. – Hivaga Apr 27 '18 at 08:57
  • Is there any reason why the parent isn't handling the children himself ? – CornelC Apr 27 '18 at 09:03
  • @Is there any reason why the parent isn't handling the children himself ? – CornelC There is no reason and I could do this but how can a parentComponent select its children based on the abstract Class which they extend ? – Hivaga Apr 27 '18 at 13:10

0 Answers0