elasticrash elasticrash - 1 year ago 160
AngularJS Question

Angular 2 Child component to check parent component

what I want to achieve is different behavior of a component based on which component is its parent component. So in the following two situations the child would behave slightly different.

example A


example B


I tried to do the following

el: ElementRef) {
this.el = el;

ngAfterViewInit() {
const hostElem = this.el.nativeElement;

eventhough ngAfterViewInit is a bit late for me, I still got null on the parentNode

Is there any way to achieve something like that?

Answer Source

If you need the actual component (and not the node element) you can use the injector.

constructor(private parentB: ParentBComponent){

ngAfterViewInit() {

Note that you have parent-a and parent-b, if you want to be able to inject any, define a base component and have both parent-a and parent-b inherit from it.

You can make it optional, you can use the Optional decorator:

constructor(@Optional() private parentB?: ParentBComponent){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download