-2

I need to post same url with multiple parameters simultaneously . how to achieve in angular 1

zgue
  • 3,417
  • 9
  • 31
  • 35
sreeraj
  • 41
  • 1
  • 8

1 Answers1

3

You do it with $http service with $http.post() request. If you want to do multiple requests, you can do it a loop. E.g. return $http.post(url,data).then((response)=>{return response.data;})

This is where you need to clarify what do you mean by saying "simultaneously", because if you want to receive the response from all of these requests at the same time, then you need something more advanced like deferred objects. This is what $q service is for, it helps you to resolve such Promises.

Firstly you need to collect all of the asynchronous callbacks in an array:

var promises = [];
angular.forEach(array, function(element) { 
  promises.push( $http.post(url,element).then((res)=>{return res.data}) );
}

(Pass different parameters/data however you like)

Then you need to resolve all of them at the same time with $q.all():

$q.all(promises).then((res)=>{ 
  /* do what you need with them, e.g:
     $q.defer().resolve(res)
  */
})

It should resolve an array with your data from previous requests in synch now.

Aleksey Solovey
  • 4,034
  • 3
  • 12
  • 31