Monday, 29 May 2017

Calling Child Component Method in Angular2


One of my current tasks in the development of the newer app version (Angular 2) is to call the child component from parent component. The logic is when a button is clicked and certain conditions are met then it calls the child component’s method and inside that method (child component), I need to display a div that contains a form to be filled up by the user.
I thought that by simply calling the child method, I will achieve what is required to do but it did not and here is what I have been doing wrong.

//inside parent component
openTab(){
this.childcomponent.showPage(2, ‘2’);
}

On this code, I was calling the child component’s method without injecting to it. So this code will not work the way I was expecting.
It took days for me to look for a solution on how to do it right until I found a guide from Stack Overflow that also points to angular.io’s Component Communication

I then understood what should be done. What exactly I have been looking for as a solution is to inject the child component to parent as a ViewChild where I can directly inject to child component’s method. I thought that the issue I face will be a never-ending one, but I was able to resolve it after reading the thread from Stack Overflow and angular.io.



No comments:

Post a Comment