我有一个主 Controller 和另外两个 Controller :
<div ng-controller="mainController" ng-model="value">
<div ng-controller="otherController"></diV>
<div ng-controller="anOtherController"></div>
</div>
由于 Controller 继承,我的 otherController
可以更新我的 value
我与 ng-model
指令绑定(bind)。
但是我如何才能注意到我的 anOtherController
的 值
已更改,以便执行 anOtherController 拥有的函数?
我可以将函数注册到该值吗?
最佳答案
您可以在 Controller 中执行此操作..
$scope.$watch("value",function(newValue,oldValue){
// your code goes here...
});
它基本上会监视给定“范围属性”的任何更改。但是,我的建议是使用服务或工厂。
请参阅其他 SO 讨论:
关于angularjs - 当值改变时如何执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23085976/