Matt Wilson
Matt Wilson

Reputation: 8309

Call method / trigger an action on a child component in Angular 1.x

I am trying to utilise component in Angular 1.6 with:

What is the best approach for calling functions in the child component from the parent component?

In my scenario, my child component will represent a modal dialog with a showModal() function. I want to pass in an ID for the item I want to edit (easy enough with a < input binding) and then call the child component's showModal() method from the parent component (item list).

Upvotes: 0

Views: 866

Answers (2)

a better oliver
a better oliver

Reputation: 26828

Simply add an additional binding to control the visibility of the dialog:.

'visible': '<'

$onChanges(changes) {
  if(changes.visible && changes.visible.currentValue) {
   this.showDialog();
  }
}

Upvotes: 1

khajaamin
khajaamin

Reputation: 876

Hey May be you know already but let me point one more most common thing which is broadcasting event from parent to child this works best.

if you know broadcasting events with data like this

  $rootScope.$broadcast('resetDataRange',{id:1}); 

this you should write into your parent component when you want to call method of the child

In Child

      $rootScope.$on("resetIntraDataRange",function(data){

showModal(data.id); 

                });

this will call when broadcasting event triggered and you will get what you want into child .

100% working solution

Upvotes: 0

Related Questions