Javascript 使用foreach完成多个http调用后的Angular js回调
我正在发送多个HTTP调用来更新foreach循环中的项目,并且在所有请求完成后需要回调。我找到了,但没有帮助 我的代码:Javascript 使用foreach完成多个http调用后的Angular js回调,javascript,angularjs,Javascript,Angularjs,我正在发送多个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"); //sho
$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
关键字,因为它必须返回一个承诺(当然,that.post()
函数也必须返回一个承诺):
那么这应该是可行的:
var promises = [];
_($scope.students.items).each(function(item) {
promises.push($scope.student.update());
})
$q.all(promises).then(function() {
alert("complete");
});
在
update()
函数中缺少return
关键字,因为它必须返回一个承诺(当然,the.post()
函数也必须返回一个承诺):
那么这应该是可行的:
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");
});
我已经用下面的代码片段更新了这里的代码。我使用了一些返回简单承诺的方法。包括两种方法可以做到这一点
- 将承诺推入数组并使用q.all
- 将map与q.all一起使用
angular.module('demoApp',[]).controller('DemoController',function($scope,$q,$timeout){
var a=函数(){
var deferred=$q.deferred();
log('Executing a');
延迟。解决();
回报。承诺;
};
var b=函数(){
var deferred=$q.deferred();
log('Executing b');
延迟。解决();
回报。承诺;
};
var c=函数(){
var deferred=$q.deferred();
log('Executing c');
延迟。解决();
回报。承诺;
};
变量f=[{
电话:a
}, {
电话:b
}, {
电话:c
}];
$scope.mapTest=函数(){
$q.all(f.map)(功能(项目){
return item.call();
})).然后(函数(){
控制台日志(“完成”);
});
};
$scope.promisePush=函数(){
var承诺=[];
角度。forEach(f,函数(项){
promises.push(item.call());
});
$q.all(承诺)。然后(函数(){
console.log('complete');
});
};
});代码>
所有人都使用地图
所有人都使用承诺推送
您也可以使用map
$q.all(_($scope.students.items).map(function(item) {
item.update();
})).then(function() {
alert("complete");
});
我已经用下面的代码片段更新了这里的代码。我使用了一些返回简单承诺的方法。包括两种方法可以做到这一点
- 将承诺推入数组并使用q.all
- 将map与q.all一起使用
angular.module('demoApp',[]).controller('DemoController',function($scope,$q,$timeout){
var a=函数(){
var deferred=$q.deferred();
log('Executing a');
延迟。解决();
回报。承诺;
};
var b=函数(){
var deferred=$q.deferred();
log('Executing b');
延迟。解决();
回报。承诺;
};
var c=函数(){
var deferred=$q.deferred();
log('Executing c');
延迟。解决();
回报。承诺;
};
变量f=[{
电话:a
}, {
电话:b
}, {
电话:c
}];
$scope.mapTest=函数(){
$q.all(f.map)(功能(项目){
return item.call();
})).然后(函数(){
控制台日志(“完成”);
});
};
$scope.promisePush=函数(){
var承诺=[];
角度。forEach(f,函数(项){
promises.push(item.call());
});
$q.all(承诺)。然后(函数(){
console.log('complete');
});
};
});代码>
所有人都使用地图
所有人都使用承诺推送
$scope.student.items
中每个项目的更新功能必须返回承诺才能正常工作。比如:
function update() {
return $http( ... );
}
$scope.student.items
中每个项目的更新功能必须返回承诺才能正常工作。比如:
function update() {
return $http( ... );
}
您不应该在foreach中发送相同的ajax。这应该是单个ajax,更新应该在回调中完成,所以发送一个类似“studentsUpdate”的ajax,作为响应,在studenst集合和更新对象数据上执行foreach。好的做法是减少ajax调用。您不应该在foreach中发送相同的ajax。这应该是一个ajax,更新应该在回调中完成,所以发送一个类似“studentsUpdate”的ajax,并响应studenst收集和更新对象数据做foreach。好的做法是减少ajax调用。在这里发布$scope.student.update()
函数的代码。为什么要调用常规$scope.student.update()
不使用项
?这是对api服务的一般调用,如果更新此学生后有助于解决问题,我也可以使用回调。如下$scope.student.update(函数(){callback here})@Rhumborl否我需要项来设置学生属性我已删除了代码以保持问题的简单性在此处放置$scope.student.update()
函数的代码。为什么要调用常规$scope.student.update()
不使用项
?这是对api服务的一般调用,如果更新此学生后有助于解决问题,我也可以使用回调。如下$scope.student.update(函数(){callback here})@Rhumborl否我需要项目来设置学生属性我已删除代码以保留我的问题simple@jitender这实际上取决于函数$scope.student.update()代码>看起来像-它必须返回承诺。。。我也不明白为什么这个函数不带任何参数?@Akarienta这是一个通用的服务,非常重要explain@jitender重要的是这个函数返回什么?这个函数将返回学生模型update@jitender这是承诺吗?我认为问题出在$scope.student.update()中代码>功能;你的代码里没有posted@jitender这实际上取决于函数$scope.student.update()代码>看起来像-它必须返回承诺。。。我也不明白为什么这个函数不带任何参数?@Akarienta这是一个通用的服务,非常重要explain@jitenderth