【发布时间】:2013-11-30 05:00:03
【问题描述】:
以this Plnkr 为例。我不知道会预先创建多少fooCollection 的成员。所以我不知道会有多少bar 模型存在。
但我知道它们将是角度模型,并且我知道它们将在哪里。
我如何在这些上设置$watch?
我需要这样做,因为我需要在 bar 模型更改时触发行为。只看 fooCollection 本身是不够的,$watch 监听器不会在 bar 更改时触发。
相关html:
<body ng-controller="testCtrl">
<div ng-repeat="(fooKey, foo) in fooCollection">
Tell me your name: <input ng-model="foo.bar">
<br />
Hello, my name is {{ foo.bar }}
</div>
<button ng-click="fooCollection.push([])">Add a Namer</button>
</body>
相关JS:
angular
.module('testApp', [])
.controller('testCtrl', function ($scope) {
$scope.fooCollection = [];
$scope.$watch('fooCollection', function (oldValue, newValue) {
if (newValue != oldValue)
console.log(oldValue, newValue);
});
});
【问题讨论】:
标签: angularjs angularjs-scope angularjs-ng-repeat