Crocsx
Crocsx

Reputation: 7589

call a function in parent of parent angular 4

I have a child of a child component

I want to call a function in the parent

CompA CompB CompC

My CompC want to call a function in CompA

Do I have to add output to C that call a function in B that output to B and call a function in A ?

Is there a more direct way?

Upvotes: 0

Views: 64

Answers (2)

Harun Or Rashid
Harun Or Rashid

Reputation: 5927

Inject parent component using forwardRef and call parent f() from child as below:

constructor(@Inject(forwardRef(() => ParentComponent)) public _parent: ParentComponent){}

ngOnInit(){
    this._parent.f();
}

Upvotes: 0

Suresh Kumar Ariya
Suresh Kumar Ariya

Reputation: 9764

Agree with @chellappan comments on create behaviorsubject and emit events. You can also achieve using DI.

You can import CompA in Compc and call like this.

export class ChildComponent implements OnInit {

  constructor(private appc: AppComponent){}

  ngOnInit(){
    this.appc.callfromChild();
  }
}

https://stackblitz.com/edit/angular-bjmxe6

Upvotes: 2

Related Questions