2016-08-16 16 views
0

私は選択型の複数の入力を持つコンポーネントを構築していますが、必要なオプションがあります。必要なフィールドが使用されると、requiredFieldsの配列から削除され、requiredFieldsRemoved配列に追加されます。もし私の心が変わり、selectの別のオプションが選択され、私は必須ではないフィールドを選択した場合、requiredFieldsRemovedにプッシュされた削除されたオブジェクトを返し、元のrequiredFields配列に戻す必要があります。Angularjs - オブジェクトの以前の状態を取得する

私がしたことは、変更されたオブジェクトを返す入力選択を追加することです。そのため、選択することができるさまざまな変更について前の状態を保存する必要があります。

//@Param publicationObject: change object from UI 
$scope.itemValue = function (publicationObject) { 

    // get the index using lodash 
    var idx = lodash.findKey($scope.requiredFields, { 
     name: publicationObject.name 
    }); 

    // here I have to check whether the object already suffered 
    // changes above or if it's his first change. 
    // if it have change previously, i will check if that status match 
    // to any object of the requiredFieldsRemoved array 
    // and push it again to the requiredFields array, if not i just do nothing. 
    // 

    //if index is found, delete the item from required fields array 
    if(idx !== undefined) { 
     $scope.requiredFields.splice(idx, 1); 
     //add removed item to the requiredFieldsRemoved array 
     requiredFieldsRemoved.push(publicationObject); 

    } 
}; 

これまでのステータスを取得するために実装できるものは何ですか?

+0

$ scope.itemValue =関数(publicationObject){ VAR OLDVALUE = angular.copy(publicationObject) }を使用してみてください。 –

+0

編集する前に古い値を保存するためにangular.copyを使用してみてください –

答えて

0

angular.copy

$scope.itemValue = function (publicationObject) { 
    var oldValue = angular.copy(publica‌​tionObject) 
    //code 
    }; 
関連する問題