mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

do Restangular post in loop #1458

Closed atefcharef closed 7 years ago

atefcharef commented 7 years ago

I need to call a list of restangular.post inside of a loop, to add many user (for example)

I have a factory that do something like that:

createUser (user) { return Restangular.all(URL).post(user); }

then in controller In need to call this factory inside of a loop:

for( var us in users){ createUser(us); }

the problem is that I cant do this! it add always only the last user object!, so how to do $q.all() using Restangular, or there are any other solution??

bostrom commented 7 years ago

You can use $q.all to make multiple requests:

var promises = [];
for (var us in users) {
  promises.push(Restangular.all(URL).post(us);
}
$q.all(promises).then(function (responses) {
  // responses is an array with one return value per request made
});

Does that work for you?

atefcharef commented 7 years ago

thank you it works!