How to get an object that was changed in angularjs?

纵饮孤独 提交于 2019-12-05 06:06:39

I don't see a way currently in Angular to get the changed object... I suspect you might need to traverse the new array and try to find the differences with the old array...

Edit: Note that this solution turns out to be a bad practice as it is adding a lot of watchers, which is something you do not want because it has a performance penalty.

=======

I eventually 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);
    });
});

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

I implemented undo/redo with AngularJS using $watch, mb this can 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];
            }
        }
    }
})

;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!