2013-04-20 38 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

相關問題