I am trying to send multiple $http
request, the request is in .service
:
app.service('servlet', function($http){
var base = "http://myUrl.com" // real url is private
//queryParams for GET request
this.sendGet = function(queryParams){
var promise = $http({method:'GET', url:base, params:queryParams}).then(
(function(response){
return response.data;
});
return promise; // var promise
}
});
Calling sendGet()
work fine and returns a promise
but when I use it in a loop :
console.log($scope.items.length) // 5
for(var i =0 ; i<scope.items.length; i++){
//scope.items - query params
console.log(scope.items[i]);
servlet.sendGet(scope.items[i]).then(function(data){
console.log(i);
});
};
the log show :
object{param1 : 1} //scope.items[i]
object{param2 : 2} //scope.items[i]
and then the console.log(i)
- inside the servlet in the loop.
[4]5