我正在考虑在我的应用程序中实现“Amazon Wishlist-ish”撤消功能。
我的意思是...
then click 'Delete Item'
by clicking 'Undo', the deletion appears to be canceled
我的列表 Controller 当前看起来像这样,
function ListsController($scope, List) {↲
List.get({}, function(lists) {
$scope.lists = lists.objects;
$scope.delete_list = function(index) {
var isConfirmed = confirm('Are you sure you want to delete it?');
if (isConfirmed) {
var targetlist = $scope.lists[index];
List.delete({ listId: targetlist.id },
function(list) {
$scope.lists.splice(index, 1);
}
)
}
}
});
};
但我想像我说的那样启用撤消功能。
在 Angular JS 中做到这一点的最佳方法是什么?
最佳答案
如果没有看到 List
服务的作用,我无法给出确切的示例,但我认为最好的选择是跟踪另一个作用域变量中已删除的项目,并且如果单击了撤消操作只需将其添加回列表即可。
也许是这样的:
$scope.deleted_lists = [];
$scope.delete_list = function(index) {
var isConfirmed = confirm('Are you sure you want to delete it?');
if (isConfirmed) {
var targetlist = $scope.lists[index];
List.delete({ listId: targetlist.id }, function(list) {
$scope.deleted_lists.concat($scope.lists.splice(index, 1));
});
}
};
然后,您可以使用 ng-repeat(如果您想要多次撤消)来显示已删除的项目,并且单击撤消按钮可以简单地通过 .push() 将项目添加回列表。
关于javascript - 在 AngularJS 中像撤消一样实现 Amazon Wishlist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23667951/