Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  51] [ 7]  / answers: 1 / hits: 70806  / 9 Years ago, tue, june 23, 2015, 12:00:00

I have a nested child component like this:





<app-main>
<child-component />
</app-main>


My appMain component needs to invoke a method on child-component.



How to invoke a method on the child-component?


More From » angular

 Answers
15

You can get a reference to an element using



@ViewChild('childComponent') child;


where childComponent is a template variable <some-elem #childComponent>` or



@ViewChild(ComponentType) child;


where ComponentType is the type of a component or directive and then in ngAfterViewInit or an event handlers call child.someFunc().



ngAfterViewInit() {
console.log(this.child);
}


See also get hold of an element in the template


[#66078] Sunday, June 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristopherw

Total Points: 173
Total Questions: 107
Total Answers: 98

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
kristopherw questions
;