【问题标题】:chained angularjs $http requests within for each loop每个循环内的链接 angularjs $http 请求
【发布时间】:2015-09-09 22:27:43
【问题描述】:

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

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

非常感谢任何帮助/建议。提前致谢。

这里是sn-p -

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

【问题讨论】:

  • 请不要将sn-ps用于不需要运行的代码。
  • 完成。感谢您的建议:-)
  • 我已经为你修好了...

标签: javascript angularjs-http


【解决方案1】:

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

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

【讨论】:

  • 这正是我所需要的。工作喜欢一种魅力。谢谢:-)
【解决方案2】:

代码有点含糊,无法给出一个好的答案,但我假设您在chainedAjax-function 中的外部 $http-call 是您想要检测何时执行 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 实现 :-) 谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多