2017-04-27 31 views
0

我在scope变量这样的存储数据比较两个数组和获取数据,因为它是

$scope.myData = 
    { 
     "firstName": "rocky", 
     "lastName": "P", 
     "place": "Koraput", 
     "education": [ 
     { 
      "id": "764006" 
     }, 
     { 
      "id": "764001" 
     } 
     ], 
     "email": "[email protected]", 
     "id": "46ads75asda7s6d57ad" 
    } 

案例:假设我更新这个数据。我添加了教育,然后点击cancel。如何删除当前添加的教育点击取消和检索数据,只有两个教育如上所述点击edit user

+0

请编辑的问题。目前还不清楚你想问什么。 – ScrapCode

+0

你可以有一个你的初始数组的副本,当编辑和恢复到取消可能吗? – tanmay

+0

使用'.push()'将数据添加到您的教育中,这会将数据添加到数组的末尾。如果取消,则使用'.pop()'。这将删除您最后添加的数据。 – Jalil

回答

0

您可以使用angular.copy()方法有原始数组的一个副本对象,只是让对它的引用,当你取消

var app = angular.module('demoApp', []); 
 

 
app.controller('demoCtrl', function($scope) { 
 
    $scope.myData = { 
 
    "firstName": "rocky", 
 
    "lastName": "P", 
 
    "place": "Koraput", 
 
    "education": [{ 
 
     "id": "764006" 
 
    }, { 
 
     "id": "764001" 
 
    }], 
 
    "email": "[email protected]", 
 
    "id": "46ads75asda7s6d57ad" 
 
    }; 
 
    $scope.copy = angular.copy($scope.myData.education); 
 
    $scope.onAdd = function() { 
 
    $scope.myData.education.push({ 
 
     id: $scope.myData.education.length 
 
    }); 
 

 
    }; 
 
    $scope.onCancel = function() { 
 
    $scope.myData.education = $scope.copy; // <----reset to original 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script> 
 
<div ng-app="demoApp" ng-controller="demoCtrl"> 
 
    <pre>{{myData.education}}</pre> 
 
    <button ng-click="onAdd()">+</button> 
 
    <button ng-click="onCancel()">X</button> 
 
</div>

1

你应该保持两个独立对象,一个是原始的,未改变的对象,另一个用于编辑。一旦用户点击,说save,那么你应该用第二个对象覆盖第一个对象。单击cancel后,只需将可编辑对象的值恢复为原始数据的克隆即可。

// Your original data (unchanged) 
$scope.myData = { /* ... */ }; 

// Your object for editing purposes 
$scope.myDataClone = clone($scope.myData); 

$scope.cancel = function() { 
    // reset the 'editable' clone to the unchanged value of myData 
    $scope.myDataClone = clone($scope.myData); 
} 

$scope.save = function() { 
    // Once the user accepts their changes, you can simply 
    // set the value of myData to a clone of the edited data. 
    // This will ensure you are not just creating a new pointer 
    // from myData to myDataClone, which would cause myData 
    // to change if you make subsequent requests to myDataClone. 
    $scope.myData = clone($scope.myDataClone); 
} 

// A clone function which takes an object and returns an exact 
// replica as a new object with a unique memory reference 
function clone(obj) { 
    return JSON.parse(JSON.stringify(obj)); 
} 
+0

保留原始副本,并在取消作品时对其进行比较。 –

+0

这是你要找的答案吗? –

+0

是的。而不是克隆(),我用angular.copy()@Danny Bull –

0

删除使用ID

$scope.myData = 
    { 
    "firstName": "rocky", 
    "lastName": "P", 
    "place": "Koraput", 
    "education": [ 
    { 
     "id": "764006" 
    }, 
    { 
     "id": "764001" 
    } 
    ], 
    "email": "[email protected]", 
    "id": "46ads75asda7s6d57ad" 
    }; 

    //Remove specific item 
    $scope.onCancel = function(cancelId){ 

     for(var i in $scope.myData.education){ 
     if($scope.myData.education[i].id==cancelId){ 
     $scope.myData.education.splice(i, 1);; 
     break; 
     } 
    } 

    }; 
0

可以使主对象,而副本取消:

你会通过克隆的第一个对象到一个新的,第二个对象开始您可以使用副本更新主对象

并保存更新副本时使用新对象

$scope.myData = 
    { 
    "firstName": "rocky", 
    "lastName": "P", 
    "place": "Koraput", 
    "education": [ 
    { 
     "id": "764006" 
    }, 
    { 
     "id": "764001" 
    } 
    ], 
    "email": "[email protected]", 
    "id": "46ads75asda7s6d57ad" 
    }; 
$scope.copymyData = angular.copy($scope.myData); 
$scope.cancel = function(){ 
    $scope.myData = angular.copy($scope.copymyData); 
} 
$scope.save = function(){ 
    $scope.copymyData = angular.copy($scope.myData); 
} 
0

我们可以通过使用push和pop实现这一目标,

HTML:

<button ng-click="cancel()">cancel</button> 

控制器:

$scope.myData =[]; 
    $scope.myData = [ 
    { 
    "firstName": "rocky", 
    "lastName": "P", 
    "education":'MBA', 
    "place": "Koraput", 
    "email": "[email protected]", 
    "id": "46ads75asda7s6d57ad" 
    }]; 

    $scope.myData.push({ 
     education : 'BE' 
    }) 

    $scope.cancel = function(){ 
    var lastElement = $scope.myData.pop(); 
    }