Reputation: 141
In Angular 2, a child component can get its parent component injected through a constructor parameter. Example:
@Component({...})
export class ParentComponent {
...
}
@Component({...})
export class ChildComponent {
constructor(private parent: ParentComponent) { }
...
}
This works nice and well as long the parent and child are of different types.
However, another typical use case is a tree structure where each tree node is displayed as a separate component. What should we do if each of the tree node components should have access to its parent? I have tried this:
@Component({...})
export class TreeNodeComponent {
constructor(private parent: TreeNodeComponent) { }
...
}
But this fails with the following runtime exception:
EXCEPTION: Cannot instantiate cyclic dependency!
I guess the reason is that Angular 2 injects the component itself instead of its parent component.
How can I tell angular to inject a component's parent component even though they are of the same type?
Plunker https://plnkr.co/edit/ddvupV?p=preview
Upvotes: 14
Views: 6051
Reputation: 657318
This way it's working
constructor(@SkipSelf() @Host() @Optional() parent: TreeNodeComponent) {}
@SkipSelf()
is to not get oneself injected which would apply if TreeNodeComponent
is requested@Host()
don't look further up than the host element@Optional()
?? there is no parent TreeNodeComponent
for the root nodeUpvotes: 24
Reputation: 141
This post has been solved thanks to Günther. However, I would like to follow up based on the architectural feedback I got.
First and foremost: I completely agree that the TreeNodeComponent use case example is an anti pattern. A data driven component like a tree this should be controlled by data binding. My apologies for this anti pattern.
However, my actual use case (which is more complex to explain) is that I want to develop an advanced dropdown menu. Requirements:
A usage example:
<dropdown label="Actions">
<dropdown-item label="Action 1" (click)="action1()"></dropdown-item>
<dropdown-item label="Action 2" (click)="action2()"></dropdown-item>
<dropdown-item label="Submenu">
<dropdown-item label="Action 3.1" (click)="action31()"></dropdown-item>
<dropdown-item label="Action 3.2">
<dropdown-slider (change)="changeHandler()"></dropdown-slider>
</dropdown-item>
<dropdown-item label="Submenu">
<dropdown-item label="Action 3.3.1" (click)="action331()"></dropdown-item>
<dropdown-item label="Action 3.3.2" (click)="action332()"></dropdown-item>
</dropdown-item>
</dropdown-item>
</dropdown>
My consideration is that this would be hard to implement using data binding. It's much practical to be able to bind event handlers and include custom components this way.
But I might be wrong! Any opinions about this are highly welcome!
Upvotes: 0
Reputation: 202176
Angular2 looks into the current injector for a provider. In your case, TreeNodeComponent corresponds to the component itself.
The parent component instance is located into the parent injector.
I think that you could try to inject an Injector class to have access to the parent injector and then get the instance of the parent component. Something like that:
@Component({
(...)
})
export class TreeNodeComponent {
constructor(injector:Injector) {
let parentInjector = injector.parent;
let parent = patentInjector.get(TreeNodeComponent);
}
}
See this link for the documentation of the Injector class:
That said, I think that the Gunter's comment about binding and shared service is particularly relevant...
Upvotes: 0