Javascript 在for each循环中链接angularjs$http请求

Javascript 在for each循环中链接angularjs$http请求,javascript,angularjs-http,Javascript,Angularjs Http,我需要检查“for loop”函数何时完成了所有$http请求的处理,并且可以一次性地刷新数据网格。目前,每个$http请求都会刷新,这不是期望的行为 我已经读了一些关于angularjs$q.all的内容,但不确定下面场景中的实现 非常感谢您的帮助/建议。提前谢谢 以下是片段- function chainedAjax(param1, param2) { var something = something; $http({ // processing }).then(fun

我需要检查“for loop”函数何时完成了所有$http请求的处理,并且可以一次性地刷新数据网格。目前,每个$http请求都会刷新,这不是期望的行为

我已经读了一些关于angularjs$q.all的内容,但不确定下面场景中的实现

非常感谢您的帮助/建议。提前谢谢

以下是片段-

function chainedAjax(param1, param2) {
  var something = something;
  $http({
    // processing
  }).then(function(responseData) {
    // processing 
    return $http({
      // processing
    });
  }, function(ex) {
    // error
  }).then(function(responseData) {
    // processing
    return $http({
      // processing
    });
  }, function(ex) {
    // error       
  }).then(function(responseData) {
    // success - Done
    finish();
  }, function(ex) {
    // error
  });
}

function init(selectedDocs) {
  var something = something;
  angular.forEach(selectedDocs, function(item, arrayIndex) {
    chainedAjax(item.param1, item.param2);
  });
}

function finish() {
  refreshDocsTable(); // refreshes the current grid
}

init(selectedItems); // call init function

您需要这样的东西,假设您实际上需要对每个项目提出多个请求:

function chainedAjax(param1, param2) {
  var something = something;
  return $http({})
    .then(function(responseData) {
      // processing 
      return $http({});
    })
    .then(function(responseData) {
      // processing
      return $http({});
    })
}

function dealWithError(error) {}

function init(selectedDocs) {
  var something = something;
  var requests = [];
  angular.forEach(selectedDocs, function(item) {
    requests.push(chainedAjax(item.param1, item.param2));
  });
  $q.all(requests)
    .then(finish)
    .catch(dealWithError);
}

为了给出一个好的答案,代码有点模糊,但我假设chainedAjax函数中的外部$http调用是您希望在执行x次时检测的调用。似乎还有一些内部的$http调用,我假设这些是您想要摆脱的调用。你可以这样做:

function chainedAjax(param1, param2) {
  var something = something;
  return $http({
    // processing
  }).then(function(responseData) {
    // processing 

  }, function(ex) {
    // error
  }).then(function(responseData) {
    // processing

  }, function(ex) {
    // error       
  }).then(function(responseData) {
    // success - Done
    finish();
  }, function(ex) {
    // error
  });
}

function init(selectedDocs) {
  var something = something;
  var count = 0, target = selectedDocs.length - 1;
  angular.forEach(selectedDocs, function(item, arrayIndex) {
    chainedAjax(item.param1, item.param2).then(function(){
        count++;
        if(count == target){
            // All requests are done
            // Now make one final call to update datatable
            $http({
                // parameters
            }).then(function(response){

            });
        }
    }).catch(function(err){
        // A single http request failed, if you want to count that as well, uncomment the line below
        // count++;
    });
  });
}

因为$http已经返回了一个承诺,所以当请求完成时,您不需要使用$q来获得信号。如果答案指向正确的方向,请告诉我。

请不要将代码片段用于不需要运行的代码。完成。谢谢你的建议:-)我已经为你修好了……这正是我所需要的。他工作起来很有魅力。谢谢:-)看起来是$q的一个很好的替代品,但是我使用了$q实现:-)谢谢!