您可以angular.copy
用来制作您正在编辑的用户的副本。然后,当他们单击保存时,您将用户复制回来:
$scope.editUser = function (selectedUser) {
// Note: you might want to save the user's index or something so you can
// know where to return the user, like this:
$scope.userToAdd = angular.copy(selectedUser);
};
如果您在循环中呈现用户,您可以执行以下操作:
<li ng-repeat="user in users">
...
<button ngclick="editUser(user, $index);">Edit</button>
</li>
JavaScript:
$scope.editUser = function(selectedUser, index) {
// Note: you might want to save the user's index or something so you can
// know where to return the user, like this:
$scope.userToAdd = angular.copy(selectedUser);
$scope.userToAddIndex = index;
};
$scope.saveUser = function() {
// You may want to user angular.copy() here again, to prevent accidental
// modification of the user.
$scope.users[$scope.userToAddIndex] = $scope.userToAdd;
};