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

現在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 に答える