2017-04-27 66 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(); 
    }