我有删除功能,但编码不好。有时会抛出错误,因为game
早于所有里程碑被删除。如果成功删除所有里程碑,我怎样才能致电game.$remove()
?非常感谢你!
$scope.remove = function(game) {
if(game) {
var milestonesToDelete = MilestonesService.query({ gameId: game._id }, function(milestonesToDelete) {
if(milestonesToDelete) {
for(var i = 0; i < milestonesToDelete.length; i++) {
milestonesToDelete[i].$remove();
}
}
});
game.$remove();
for (var j in $scope.games) {
if ($scope.games [j] === game) {
$scope.games.splice(j, 1);
}
}
} else {
$scope.game.$remove(function() {
$location.path('admin/games');
});
}
};
答案 0 :(得分:3)
在里程碑查询完成后,您似乎只需要删除游戏。所以只需将代码放在异步回调中:
$scope.remove = function(game) {
if(game) {
var milestonesToDelete = MilestonesService.query({ gameId: game._id }, function(milestonesToDelete) {
if(milestonesToDelete) {
for(var i = 0; i < milestonesToDelete.length; i++) {
milestonesToDelete[i].$remove();
}
}
game.$remove();
for (var j in $scope.games) {
if ($scope.games [j] === game) {
$scope.games.splice(j, 1);
}
}
});
} else {
$scope.game.$remove(function() {
$location.path('admin/games');
});
}
};