AngularJS: loop POST requests and pass each index into related response

后端 未结 1 1924
一整个雨季
一整个雨季 2021-01-19 08:28

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:

v         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-19 09:19

    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.

    0 讨论(0)
提交回复
热议问题