Fractaliste Fractaliste -3 years ago 91
AngularJS Question

How to execute a function when a value change?

I've a main controller and into it two other controllers:

<div ng-controller="mainController" ng-model="value">
<div ng-controller="otherController"></diV>
<div ng-controller="anOtherController"></div>
</div>


Thanks to the controller inheritance my
otherController
can update my
value
I bind with the
ng-model
diretive.

But how can I notice my
anOtherController
that the
value
have changed in order to execute a function owned by anOtherController?

Can I register a function to this value?

Answer Source

You can do this in your controller..

$scope.$watch("value",function(newValue,oldValue){
 // your code goes here...
});

It will basically watch for any changes on a given "scope property". However, my advice is to use a either a service or a factory.

Please refer to this other SO Discussion:

AngularJS Service Passing Data Between Controllers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download