Sending post request in for loop

前端 未结 4 1021
后悔当初
后悔当初 2021-01-05 11:59

I would like to send post requests in loop. If i send for example 2 requests in a row only the last request really made the callback.

What am i do wrong?

         


        
4条回答
  •  天命终不由人
    2021-01-05 12:30

    Its clearly a closure issue. Read more here

    Also it's suggested using $resource over $http. (ng-resource).

    Check out the example to post in for loop using resource.

          for(var i=0; i<$scope.ListOfRecordsToPost.length; i++){       
              var postSuccessCallback = function(postRec) { 
                  console.info('Posted ' + postRec);
              }($scope.ListOfRecordsToPost[i]);
    
              lsProductionService.post({}, postSuccessCallback); 
          }
    

提交回复
热议问题