如何深观看angularjs一个数组? [英] How to deep watch an array in angularjs?

查看:100
本文介绍了如何深观看angularjs一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在我的范围对象的数组,我要看每个对象的所有值。

There is an array of objects in my scope, I want to watch all the values of each object.

这是我的code:

function TodoCtrl($scope) {
  $scope.columns = [
      { field:'title', displayName: 'TITLE'},
      { field: 'content', displayName: 'CONTENT' }
  ];
   $scope.$watch('columns', function(newVal) {
       alert('columns changed');
   });
}

但是,当我修改的值,例如我换标题 TITLE2 警报(列改为')从来没有弹出。

如何看深内部数组对象?

How to deep watch the objects inside an array?

有一个现场演示: http://jsfiddle.net/SYx9b/

推荐答案

您可以 $观看的第三个参数设置为真正

You can set the 3rd argument of $watch to true:

scope.$watch('data', function (newVal, oldVal) { /*...*/ }, true);

请参阅http://docs.angularjs.org/api/ng.$rootScope.Scope#$watch

由于角1.1.x中你也可以用$ watchCollection观看浅腕表收藏(只是第一级)。

Since Angular 1.1.x you can also use $watchCollection to watch shallow watch (just the "first level" of) the collection.

scope.$watchCollection('data', function (newVal, oldVal) { /*...*/ });

请参阅https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watchCollection

这篇关于如何深观看angularjs一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆