mgonto/restangular

do Restangular post in loop

atefcharef opened this issue · 2 comments

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??

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?

thank you it works!