【发布时间】:2016-07-13 22:36:13
【问题描述】:
我正在发送多个 HTTP 调用以更新 foreach 循环内的项目,并且在所有请求完成后需要回调。我找到了this,但没有帮助。
我的代码:
$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);
});
};
有什么建议吗?
【问题讨论】:
-
在这里贴一个
$scope.student.update()函数的代码。 -
你为什么打电话给将军
$scope.student.update();而不使用item? -
这是对 api 服务的通用调用,如果对我的问题有帮助,我可以在更新此学生后使用回调。如下 $scope.student.update(function(){callback here})
-
@Rhumborl 不,我需要项目来设置学生属性我已经删除了代码以保持我的问题简单
标签: javascript angularjs