How to get an object that has been modified in angularjs?

I use this function to view an array of objects for changes:

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

How can I get the object in which the property was changed so that I can click it in an array? For example:

 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); } 
+6
object angularjs get watch
Apr 20 '13 at 17:22
source share
3 answers

I don’t see the way currently in Angular to get the modified object ... I suspect you might need to cross the new array and try to find the differences with the old array ...

+2
Apr 21 '13 at 9:04 on
source share
— -

Change Please note: this solution turns out to be bad practice, as it adds a lot of observers, which you do not need, because it has a performance limit.

=======

In the end, I came up with this solution:

 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); }); }); 
+2
Jun 03 '13 at 20:54 on
source share

There is still no such parameter for $ watch, but you can use the jQuery plugin for this, http://archive.plugins.jquery.com/project/jquery-diff

I implemented undo / redo with AngularJS using $ watch, mb this may help

 //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]; } } } }) 

;

0
Apr 21 '13 at 12:21
source share



All Articles