I am trying to use the $q
service inside my controller. I have multiple environment which I need to hit and collect the data and consolidate the data and then put inside the scope. I have the below code.
var results=[];
for(var environment in $rootScope.envs){
$http.get(callService.getDomainUrl()+'/'+$rootScope.envs[environment]+ '/hosts.json').success(function(data){
results.push(data)
})
}
$q.all(results).then(function(){
console.log(results);
})
However, I am not sure if this is the right approach and also I need to map the result into an object of the below type:
results:{
env1: { // result data for env1},
env2: { // result data for env2},
env3: { // result data for env3},
}
How to resolve the promise and consolidate the data in the above format.