Skip to content Skip to sidebar Skip to footer

Angularjs - Collecting Multiple Get Requests Into Json Array And Then Passing To Directive

I am new to angular and been struggling how to solve my problem. I need to access API multiple times for users data, store everything as JSON array and when all the data is collec

Solution 1:

This is a great place to unleash the power of $q. You can wait for all the promises to resolve and then process them using $q.all method. It simply Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.

See this example:

students = data;
var promises = [];
for (i = 0; i < students.length; i = i + 1) {

    promises.push($http.get("**** link here ****/quest/" + students[i].id));

}

$q.all(promises).then(function(response) {
    for (var i = 0; i < response.length; i++) {
         $scope.allData.push({
             id: response[i].data.id,
             value: response[i].data.length
         });
    }
})

See it in action here: http://plnkr.co/edit/TF2pAnIkWquX1Y4aHExG?p=preview

Post a Comment for "Angularjs - Collecting Multiple Get Requests Into Json Array And Then Passing To Directive"