2013-04-20 31 views
6

我用这个功能来观看对象的更改的数组:如何获取angularjs中已更改的对象?

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

我怎样才能得到哪个属性已经改变,这样我可以在一个阵列推对象? 例如:

var myApp = angular.module("myApp", []); 

myApp.factory("Data", function(){ 
var Data = [{id:1, property: "Random"}, {id:2, property: "Random again"}]; 
return Data; 
}); 

var myBigArray = []; 

function tableCtrl($scope, Data){ 
    $scope.TheData = Data; 
    $scope.$watch("TheData", function() { 

    //Here an object should be pushed 
    myBigArray.push(">>Object in which property has been changed <<<"); 
    }, true); 
} 
+1

将有助于看的项目是如何改变。在那时可能有权访问该对象。显示的代码太简单了。创建一个演示,显示用例。另外为什么你需要将数组存储为角度外的全局数据? – charlietfl 2013-04-20 18:21:49

回答

2

我不明白的方式目前在角得到改变的对象......我怀疑你可能需要遍历新阵列,并设法找到与旧阵列的差异。 ..

+0

现在是2015年。这个主题有没有更新?我花了整整一天的时间搜索如何检测正在被改变的对象,但没有运气。讨厌使用深度观看和手动比较。 – Codism 2015-09-25 03:29:49

0

还有这样没有选项$手表,但你可以使用jQuery插件的是,http://archive.plugins.jquery.com/project/jquery-diff

我实现了撤销/使用$腕表AngularJS重做,MB这可以帮助

//History Manager Factory 
.factory('HistoryManager', function() { 
    return function(scope) { 

     this.container = Array(); 
     this.index = -1; 
     this.lock = false; 

     //Insert new step into array of steps 
     this.pushDo = function() { 
      //we make sure that we have real changes by converting to json, 
      //and getting rid of all hash changes 
      if(this.container.length == 0 || (angular.toJson(scope.widgetSlider) != angular.toJson(this.container[this.index][0]))) { 
       //check if current change didn't came from "undo" change' 
       if(this.lock) { 
        return; 
       } 
       //Cutting array, from current index, because of new change added 
       if(this.index < this.container.length-1) { 
        this.container = this.container.slice(0, this.index+1); 
       } 

       var currentStepSlider = angular.copy(scope.widgetSlider); 
       var selectedWidgetIndex = scope.widgetSlider.widgets.indexOf(scope.widgetCurrent); 
       //Initialising index, because of new "Do" added 
       this.index = this.container.length; 
       this.container.push([currentStepSlider, selectedWidgetIndex]); 

       if (this.onDo) { 
        this.onDo(); 
       } 
      } 
     } 


     //Upon undo returns previous do 
     this.undo = function() { 
      this.lock = true; 
      if(this.index>0){ 
       this.index--; 
       scope.widgetSlider = angular.copy(this.container[this.index][0]); 
       var selectedWidgetIndex = this.container[this.index][1]; 
       scope.widgetCurrent = scope.widgetSlider.widgets[selectedWidgetIndex]; 
      } 
      this.lock = false; 
     } 

     //Upon redo returns next do 
     this.redo = function() { 
      if(this.index < this.container.length-1) { 
       this.index++; 
       scope.widgetSlider = angular.copy(this.container[this.index][0]); 
       var selectedWidgetIndex = this.container[this.index][1]; 
       scope.widgetCurrent = scope.widgetSlider.widgets[selectedWidgetIndex]; 
      } 
     } 
    } 
}) 

;

2

编辑:请注意,此解决方案原来是一种不好的做法,因为它加入了很多观察家的,这是你不想因为它有性能损失的东西。

=======

我最终想出了这个解决方案:

items.query(function (result) { 
    _(result).each(function (item, i) { 
     $scope.items.push(item); 
     $scope.$watch('items[' + i + ']' , function(){ 
      console.log(item); // This is the item that changed. 
     }, true); 
    }); 
}); 
+0

我知道这个答案已经超过2年了,但它出现在谷歌搜索,所以我要添加一个警告。您需要非常小心地添加多少手表。如果列表变得太大,为列表中的每个项目添加一个手表可能会真的减慢您的应用程序。 – 2015-06-11 20:39:28

+1

@D_Naish我完全同意。那时,当我发布答案时(我认为还有很多人)并没有意识到手表可能会遇到的问题。有趣的是,看看事情变化有多快。感谢您的评论! – 2015-06-12 07:08:53

相关问题