Skip to content Skip to sidebar Skip to footer

Angularjs: Loop Post Requests And Pass Each Index Into Related Response

I am trying to use AngularJS to execute multiple http POST requests and I need to create an object of successfully finished requests - something like this: var params = [1, 2, 3],

Solution 1:

This will do the trick:

var params = [1, 2, 3],
    url,
    i,
    done = {};

for (i in params) {
    (function(p) {
        url = '/dir/'+ params[p];
        $http.post(url, {"some_request": "not important"}).
            success(function(response) {
                done[params[p]] = 'successful';
            });
    })(i);
}

Another option is closure.

Post a Comment for "Angularjs: Loop Post Requests And Pass Each Index Into Related Response"