6

我使用这个函数来观察一组对象的变化:

$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);
}
4

3 回答 3

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);
    });
});
于 2013-06-03T20:54:17.837 回答
2

我目前在 Angular 中看不到获取更改对象的方法...我怀疑您可能需要遍历新数组并尝试找到与旧数组的差异...

于 2013-04-21T09:04:30.987 回答
0

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

我使用 $watch 使用 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];
            }
        }
    }
})

;

于 2013-04-21T12:21:43.120 回答