Angularjs - 获取对象的先前状态

时间:2016-08-16 20:24:54

标签: javascript angularjs json

我正在构建一个组件,其中我有多个select类型的输入,它们具有所需的选项。使用必填字段时,它将从requiredFields数组中删除并添加到requiredFieldsRemoved数组中。如果我改变主意并选择了另一个选项,我选择一个不需要的字段,我必须将已删除的对象推送到requiredFieldsRemoved并返回到它原来的requiredFields数组。

我所做的是添加更改输入选择,返回更改的对象,所以我现在需要的是保存先前关于选择可能具有的不同更改的状态。

//@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);

    }
};

我可以通过什么方式来获取以前的状态?

1 个答案:

答案 0 :(得分:0)

尝试使用angular.copy

$scope.itemValue = function (publicationObject) {
    var oldValue = angular.copy(publica‌​tionObject) 
    //code
    };