Introduction
In this article, I will discuss how to call child component method from parent component and parent component method from child component. For this article, I have created a demo project using Angular 12. In this demo project, I will create a simple parent and child component and I will demonstrate to call both methods vice versa.
Prerequisite
First of all, we have created a project using the following command in the Command Prompt.
Open project in visual code studio using following command
Call Child Component method from Parent Component
We can use Viewchild to call Child Component method from Parent Component
Viewchild
A ViewChild is a decorator which is used when we want to access a child component inside the parent component, we use the decorator @ViewChild() in Angular.
Here I have declared a variable name child as decorator @ViewChild(). As shown below we can access childMethod of child component inside the parent component.
Use following code for parent component
Use following code for child component
Call Parent Component method from Child Component
For Calling Parent Component method from Child Component, I had created a method getParentMethod and in this method, I had set input property in HTML file of parent component. Then shown as below code inside the child component I am able to access parent method.
Use following code for parent component
Use following code for child component
App.component.html
App.component.ts
child.component.html
child.component.ts
Now, run the project using the following command.
![]()
Summary
In this article, I have discussed how to Call Child Component method from Parent Component and Parent Component method from Child Component.