【发布时间】:2013-10-31 20:20:41
【问题描述】:
我如何延迟函数的执行,直到在我的所有$resources 都已解决?我的目标是能够解析 log 数组,毕竟 $resources 已解决并将单个成功通知推送到 UI,而不是每次成功推送一个通知。
我的代码基于这个问题angular -- accessing data of multiple http calls - how to resolve the promises。我意识到$scope.promises 是空的,因为item.$save() 没有返回任何内容,但我希望您能看到我正在尝试将未解决的承诺推送到promises 数组。
$scope.save = function () {
$scope.promises = [];
$scope.log = [];
angular.forEach($scope.menuItems, function(item) {
$scope.promises.push(item.$save(function(menu){
debugger; // execution gets here 2nd
console.debug("success");
$scope.log.push(msg: 'success');
}));
}, this);
$q.all($scope.promises).then(function() {
debugger; // execution gets here 1st
console.debug("all promises resolved");
});
};
【问题讨论】:
标签: javascript angularjs q angular-resource