我试图使用$q.all
并行地进行两次API调用,并将它们的响应作为一个返回给控制器,当我在哈希中的每个promise的返回行上打破时,它们会返回预期的数据,但是当它达到$q.all
时似乎都会中断。这一切都处于ui-router状态,并且我使用resolve来为相应的控制器提供来自$q.all
的数据
它最初是用Coffeescript编写的,但这里是Javascript:
resolve: {
content: [
'APIService', function($q, $timeout, APIService) {
var firstPromise, secondPromise, promises;
firstPromise = $q.defer();
secondPromise = $q.defer();
promises = {
firstPromise: APIService.get('/some/api/call').then(function(response) {
return response;
}),
secondPromise: APIService.get('/another/api/call').then(function(response) {
return response;
})
};
$.blockUI();
$timeout(function() {
firstPromise.resolve('firstPromise');
secondPromise.resolve('secondPromise');
}, 1000);
$q.all(promises).then(function(responses) {
$.unblockUI();
return responses;
});
return false;
}
]
}
请帮帮忙?我使用的是Angular 1.3.15
答案 0 :(得分:0)
content:[
实际应该是:
resolve: {
content: function(){
// <create promise array>
return $q.all(promises);
}
}
答案 1 :(得分:0)
很难重新创建你的逻辑,但这个例子效果很好,希望会有所帮助( Angular 1.3 )
angular.module('myApp',[])
.controller('MyCtrl', function ($scope, $q, $timeout) {
$scope.name = "Superhero"
var prom1 = $q.defer();
var prom2 = $q.defer();
var promises = {
prom1: prom1.promise,
prom2: prom2.promise
};
$timeout(function () {
prom1.resolve('prom1');
prom2.resolve('prom2');
}, 1000);
$q.all(promises).then(function (responces) {
$scope.prom1 = responces.prom1;
$scope.prom2 = responces.prom2;
});
});
http://jsfiddle.net/ugja9gth/1/
真实$ http
的示例