如果可以制作$scope.Something
数组,则可以编辑副本,然后在保存更改时更新数组。它仍然提供撤消,但与您呈现它的方式相反。
在这里小提琴:http: //jsfiddle.net/GYeSZ/1/
function MyCtrl($scope) {
$scope.Something = [
{ name: "Aye", desc: new Date() },
{ name: "Bee", desc: new Date() },
{ name: "See", desc: new Date() }
];
$scope.edit = function(idx) {
$scope.copy = angular.copy($scope.Something[idx]);
$scope.idx = idx;
}
$scope.save = function() {
$scope.Something[$scope.idx] = angular.copy($scope.copy);
$scope.cancel();
}
$scope.cancel = function() {
$scope.copy = null;
$scope.idx = -1;
}
}
更新
有一种替代语法ng-repeat
可用于枚举字典以获取其密钥。使用此语法,您可以使用您在问题中描述的数据结构
在这里小提琴:http: //jsfiddle.net/GYeSZ/3/
function MyCtrl($scope) {
$scope.edit = function(key) {
$scope.copy = angular.copy($scope.Something[key]);
$scope.key = key;
}
$scope.Something = {
"a": { name: "Aye", desc: new Date() },
"b": { name: "Bee", desc: new Date() },
"c": { name: "See", desc: new Date() }
};
$scope.save = function() {
$scope.Something[$scope.key] = angular.copy($scope.copy);
$scope.cancel();
}
$scope.cancel = function() {
$scope.copy = null;
$scope.key = null;
}
}
html
<div ng-repeat="(key, value) in Something" ....>