我需要链接很多 ajax 请求。我正在尝试从不同的资源中获取数据,然后将响应合并在一起。我还想显示请求的进度状态通知。
所以我有这个代码:
app.controller('MainCtrl', function($scope,$http,$q) {
$scope.name = 'World';
$scope.progress = '';
var promises = [];
var myData = {};
var urls =['data.json','data2.json','error.json','data3.json'];
var responses =['First call','Second call','Third call','Fourh call'];
for (var i = 0; i < urls.length; i++) {
var promise = $http.get(urls[i]).then(function(result){
$scope.progress = responses[i];
$scope.pr = result.data;
console.log(result.data)});
promises.push(promise);
}
$q.all(promises).then(function(values){
console.log(values)
},function(error){
console.log(error.data)
})
});
[插销]