Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何观察数组中AngularJS的变化_Javascript_Angularjs_Angularjs Scope - Fatal编程技术网

Javascript 如何观察数组中AngularJS的变化

Javascript 如何观察数组中AngularJS的变化,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我基本上希望绑定到主干集合中的“添加”和“删除”事件。在AngularJS中,我基本上看不到这样做的方法,目前我们解决的解决方法是对数组的长度进行计算,然后手动对整个过程进行微分/重新计算。这真的是酷孩子们做的吗 编辑:特别是,观察数组的长度意味着我不容易知道哪个元素被更改了,我需要手动“diff”。以角度观察数组的方法是$watch(array,function(){},true)请详细介绍您的用例。跟踪元素持久性的解决方案之一是将ngRepeat指令与侦听元素的$destroy事件的自定义指

我基本上希望绑定到主干集合中的“添加”和“删除”事件。在AngularJS中,我基本上看不到这样做的方法,目前我们解决的解决方法是对数组的
长度
进行计算,然后手动对整个过程进行微分/重新计算。这真的是酷孩子们做的吗


编辑:特别是,观察数组的长度意味着我不容易知道哪个元素被更改了,我需要手动“diff”。

以角度观察数组的方法是
$watch(array,function(){},true)

请详细介绍您的用例。跟踪元素持久性的解决方案之一是将ngRepeat指令与侦听元素的$destroy事件的自定义指令一起使用:

<div ng-repeat="item in items" on-delete="doSomething(item)">

angular.module("app").directive("onDelete", function() {
    return {
        link: function (scope, element, attrs) {
            element.on("$destroy", function () {
                scope.$eval(attrs.onDelete);
            });
        }
    }
});

angular.module(“app”).指令(“onDelete”,函数(){
返回{
链接:函数(范围、元素、属性){
元素上(“$destroy”,函数(){
范围$eval(attrs.onDelete);
});
}
}
});

也许解决方案是创建集合类(就像主干网一样),您也可以非常轻松地挂接到事件中

我在这里所做的解决方案并不全面,但可能会给你一个关于如何做到这一点的一般性指导


我会创建子作用域并单独观察它们。 以下是一个例子:

$scope.myCollection = [];
var addChild = function()
{
  var Child = $scope.$new();
  Child.name = 'Your Name here';

  Child.$watch('name', function(newValue) {
     // .... do something when the attribute 'name' is changed ...
  });

  Child.$on('$destroy', function() {
    //... do something when this child gets destroyed 
  });


  $scope.myCollection.push(Child); // add the child to collection array

};

// Pass the item to this method as parameter, 
// do it within an ngRepeat of the collection in your views 
$scope.deleteButtonClicked = function(item)
{
  var index = $scope.myCollection.indexOf(item); //gets the item index
  delete $scope.myCollection[index]; // removes the item on the array
  item.$destroy(); // destroys the original items
}
我认为使用是一个很好的解决方案,但对你来说可能更好。不执行深度比较,只监视数组修改,如插入、删除或排序(不更新项)

例如,如果要保持属性
顺序
与数组顺序同步:

$scope.sortableItems = [
    {order: 1, text: 'foo'},
    {order: 2, text: 'bar'},
    {order: 3, text: 'baz'}
];

$scope.$watchCollection('sortableItems', function(newCol, oldCol, scope) {
    for (var index in newCol) {
        var item = newCol[index];
        item.order = parseInt(index) + 1;
    }
});

但是对于您的问题,我不知道是否有比手动浏览阵列以确定更改更好的解决方案。

您有什么具体的想法吗?$watching数组的长度有什么问题?@ganaraj Edited,我只想知道哪个元素得到了changedAngular的数据绑定,它的工作方式与事件类似;你需要做的是改变你的思维方式。“为什么你真的需要知道这一点呢?”ganaraj观察长度是不安全的。在同一个摘要周期内,被监视的阵列中可能会有多个变化,从而使监视者根本不会开火。e、 g.
array.splice(0,1,“hello”)
@fastreoad例如,我有一个东西列表(比如TODO),服务器正在等待接收已删除/添加的通知,这是一个深度监视。在大多数情况下,您可能不需要它。@Jason Als:谢谢,但这并不能告诉我删除/添加了哪个元素。我对问题进行了编辑以使其更清楚:)@ganaraj实际上你是这样做的,请参阅我对op问题的评论。这很好,但我不想让我的MVC绑定到视图中的元素,以了解我的模型中的某些内容何时发生了更改是正确的方式…?是的。另一个解决方案是创建模型和集合类,如主干中的类。