每个循环内的链式 angularjs $http 请求
chained angularjs $http requests within for each loop
我需要检查 "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-calls,我认为这些就是您想要摆脱的那些。你可以这样做:
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 已经 returns 一个承诺,你不需要使用 $q 在请求完成时得到信号。让我知道答案是否为您指明了正确的方向。
我需要检查 "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-calls,我认为这些就是您想要摆脱的那些。你可以这样做:
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 已经 returns 一个承诺,你不需要使用 $q 在请求完成时得到信号。让我知道答案是否为您指明了正确的方向。