[英]AngularJS - How can I $watch a deeply nested array?
I currently have data that looks like the following:我目前的数据如下所示:
$scope.boardLists = [{
cards: [{a:1, b:2}, {a:2, b:2}]
},
{
cards: [{a:3, b:3}, {a:4, b:4}]
},
{
cards: [{a:5, b:5}, {a:6, b:6}]
}];
How can I watch for changes in the cards array?如何观察卡片阵列的变化?
I have tried the following so far:到目前为止,我已经尝试了以下方法:
$scope.$watchCollection('boardLists', function(newVal, oldVal){
console.log(newVal);
console.log(oldVal);
});
$scope.$watch('boardLists', function(newVal, oldVal){
console.log(newVal);
console.log(oldVal);
}, true);
$watch
Depths范围$watch
深度$scope.$watch('boardLists', function(newVal, oldVal){
console.log(newVal);
console.log(oldVal);
}, true);
Use $scope.$watch('item',fn,true);
使用$scope.$watch('item',fn,true);
for a deep watch.进行深度观察。
For information, see AngularJS Developer Guide - $scope
Watch Depths .有关信息,请参阅AngularJS 开发人员指南 - $scope
Watch Depths 。
$watch is a bit expensive, you can try to use $apply $watch 有点贵,你可以试试用 $apply
$scope.$apply(function() {
// do something with the scope
$scope.boardList.push({cards:...});
});
$apply will automatically rerun your templates $apply 会自动重新运行你的模板
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.