Restangular post in loop

Issue

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

Solution

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
});

Answered By – ATEF CHAREF

Answer Checked By – Gilberto Lyons (AngularFixing Admin)

Leave a Reply

Your email address will not be published.