简体   繁体   English

AngularJS - 如何 $watch 深度嵌套的数组?

[英]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);

Scope $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.

相关问题 如何基于深度嵌套数组更新 state? - How can I update the state based on a deeply nested array? 如何使用猫鼬对深度嵌套的数组进行 $slice - How can I $slice on a deeply nested array using mongoose 如何遍历javascript中的深度嵌套对象数组? - How can I iterate through array of deeply nested objects in javascript? 如何将深度嵌套对象的属性总和移动或计算到对象数组中的顶层 - how can I either move or calculate the sum of a deeply nested object's properties to the top level in an object array 如何将深度嵌套的 object 添加到 React Reducer 中的深度嵌套数组中? - How to add a deeply nested object to a deeply nested Array in a React Reducer? 如何在反应中使用钩子更新深度嵌套的数组? - How do I update deeply nested array with hooks in react? 如何在谷歌实时数据库中定位深度嵌套的 object? - How can I target a deeply nested object in google realtime database? 如何在Immutable.js中设置深层嵌套的值? - How can I set a deeply nested value in Immutable.js? 如何将数据设置为深度嵌套数组 - How to set data to deeply nested array 如何映射深度嵌套的对象数组? - How to map through a deeply nested array of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM