我已经在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);
- Davin Tryondeferred
而不是deffered
:) - Christophe Roussy