【发布时间】:2014-02-14 03:58:44
【问题描述】:
我已经在 angularjs 中实现了 $q.all,但我无法使代码工作。这是我的代码:
UploadService.uploadQuestion = function(questions){
var promises = [];
for(var i = 0 ; i < questions.length ; i++){
var deffered = $q.defer();
var question = questions[i];
$http({
url : 'upload/question',
method: 'POST',
data : question
}).
success(function(data){
deffered.resolve(data);
}).
error(function(error){
deffered.reject();
});
promises.push(deffered.promise);
}
return $q.all(promises);
}
这是调用服务的控制器:
uploadService.uploadQuestion(questions).then(function(datas){
//the datas can not be retrieved although the server has responded
},
function(errors){
//errors can not be retrieved also
})
我认为在我的服务中设置 $q.all 存在一些问题。
【问题讨论】:
-
您看到了什么行为?它会调用你的
then(datas)吗?试试push这个:promises.push(deffered); -
@themyth92 你试过我的解决方案了吗?
-
我已经尝试过,两种方法都适用于我的情况。但我会让@Llan Frumer 作为正确答案。真的谢谢你们俩。
-
你为什么要承诺化现有的承诺? $http 已经返回了一个承诺。使用 $q.defer 是多余的。
-
是
deferred不是deffered:)