Tuesday, 10 September 2013

AngularJs chaining an undefined ajax request and output progress (also if one request fail)

AngularJs chaining an undefined ajax request and output progress (also if
one request fail)

I need to chain a lot of ajax requests. I'm trying to grab data from
different resource and then merge responses all together. I also want to
show a progress status notification of the requests.
So I have this code:
app.controller('MainCtrl', function($scope,$http) {
$scope.name = 'World';
$scope.result = '';
$scope.myData = {};
var myData = {};
var promises =['data.json','data2.json','error.json','data3.json'];
var responses =['First call','Second call','Third call','Fourh call'];
for (var i = 0; i < promises.length; i++) {
$http.get(promises[i]).then(function(result){
console.log('OK')
console.log(result)
angular.extend($scope.myData,result.data)
$scope.result = responses[i]
});
}
});
[PLUNKER]
http://plnkr.co/edit/QFIPeolKE85P5x78JtCn

No comments:

Post a Comment