您如何在角度控制器中收听子属性的变化?从那个控制者那里知道哪个孩子被改变了?
的HTML
<div ng-app ng-init='people = [{name: "bill", age: "11"}, {name: "jim", age: ""}, {name: "ryan", age: ""}]'>
<h1> msg: {{ msg }} </h1>
<table>
<tr ng-repeat='person in people'>
<td>
{{ person.name }}
<input ng-model='person.name'>
</td>
</tr>
</table>
</div>
编辑
例如,如果我想向每个具有年龄值的
.highlight
添加<td>
类,我该怎么做?我是Angular的新手,但我认为$ watch是执行此操作的正确方法?因为该值可能会在UI中更改,但更改可能来自其他位置。父控制器将需要更改值,并查明更改的值并添加类。
最佳答案
当需要深入的$ watch而不是整个对象时,您可以删除不相关的数据,从而使比较更快。
示例:(由Karl Seamon解决)
$scope.$watch(function($scope) {
return $scope.people.
map(function(obj) {
return obj.name
});
}, function (newVal) {
$scope.msg = 'person name was changed';
}, true);
实时工作示例:http://jsfiddle.net/choroshin/uhrQ4/
有关更多信息,请查看我的博客post。