繁体   English   中英

在用foreach完成多个http调用之后,Angular js回叫

[英]Angular js call back after multiple http calls complete with foreach

我正在发送多个HTTP调用以更新foreach循环内的项目,并且在所有请求完成后需要回调。 我找到了,但是没有帮助。

我的代码:

$q.all(_($scope.students.items).each(function(item) {
   $scope.student.update(); //this is an http call
})).then(function() {
   // I need a callback need here
   alert("complete"); //should be shown after all students get updated but it is
                      // called before all network calls got complete
});

这是通用更新功能

self.update = function(item, callback) {
   that.post(item, self.getUrl("update") , function(err, data) {
      if (self.formatter) {
         data = self.formatter(data);
      }
      callback(err, data);
   });
 };

有什么建议么?

您错过了update()函数中的return关键字,因为它必须返回一个promise(当然, that.post()函数也必须返回一个promise):

self.update = function(item, callback) {
   return that.post(item, self.getUrl("update") , function(err, data) {
      if (self.formatter) {
         data = self.formatter(data);
      }
      callback(err, data);
   });
 };

然后这应该工作:

var promises = [];
_($scope.students.items).each(function(item) {
   promises.push($scope.student.update());
})
$q.all(promises).then(function() {
   alert("complete");
});

您也可以通过使用map来尝试

$q.all(_($scope.students.items).map(function(item) {
  item.update();
})).then(function() {
  alert("complete");
});

我已使用以下代码段在此处更新了代码。 我使用了一些返回简单承诺的方法。 包括两种执行此操作的方法。

  • 将promise放入数组并使用q.all
  • 将地图与q.all一起使用

 angular.module('demoApp', []).controller('DemoController', function($scope, $q, $timeout) { var a = function() { var deferred = $q.defer(); console.log('Executing a'); deferred.resolve(); return deferred.promise; }; var b = function() { var deferred = $q.defer(); console.log('Executing b'); deferred.resolve(); return deferred.promise; }; var c = function() { var deferred = $q.defer(); console.log('Executing c'); deferred.resolve(); return deferred.promise; }; var f = [{ call: a }, { call: b }, { call: c }]; $scope.mapTest = function() { $q.all(f.map(function(item) { return item.call(); })).then(function() { console.log("complete"); }); }; $scope.promisePush = function() { var promises = []; angular.forEach(f, function(item) { promises.push(item.call()); }); $q.all(promises).then(function() { console.log('complete'); }); }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="demoApp"> <div ng-controller="DemoController"> <button ng-click="mapTest()">Q.all using map</button> <button ng-click="promisePush()">Q.all using promise push</button> </div> </body> 

$q.all(_.map($scope.students.items, function(item) {
   return item.update();
})).then(function() {
   //everything has completed
});

$scope.student.items每个项目的更新功能都必须返回承诺才能起作用。 就像是:

function update() {
   return $http( ... );
}

您不应该在foreach中发送相同的ajax。 这应该是单个ajax,更新应该在回调中完成,因此发送一个像“ studentsUpdate”之类的ajax,并作为响应,对studenst收集并更新对象数据。 好的做法是少用ajax调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM