angularjs - 当值改变时如何执行函数?

标签 angularjs

我有一个主 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 Service Passing Data Between Controllers

关于angularjs - 当值改变时如何执行函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23085976/

相关文章:

javascript - Angular : Promise dependant on other promises in routing

javascript - Angular ng-show 在简单的示例脚本中没有按预期工作

javascript - “Thinking in ReactJS",如果我有AngularJS背景?

javascript - 根据http请求动态更新 Angular View

javascript - 当指令 Controller 使用 Angular 读取它时,指令链接中的范围变量不会更新

javascript - 只能从复选框事件访问一个类变量

javascript - Firebase 在 JavaScript Api 中按键检索对象

angularjs - 图像未加载到带有 ftp url 的 <img> 标记中

javascript - url 中不需要的哈希 '#',在 angularjs 中使用 $routeProvider

javascript - 验证失败时,首次点击时不会形成表单数据对象;仅在第二次单击时