使用 Angular.js 和 Restangular,如何在循环内的所有子请求完成时进行 Hook ?例如:
Restangular.all('clusters').getList().then(function(clusters) {
clusters.forEach(function(cluster, index) {
cluster.get().then(function(response) {
//some logic
});
});
});
本质上,我需要知道对 cluster.get()
的所有子请求何时完成,然后执行某些操作。有没有一种干净的方法可以做到这一点?
最佳答案
您应该能够使用 $q.all等待所有请求的方法。它会像这样工作:
Restangular.all('clusters').getList().then(function(clusters) {
var promises = clusters.map(function(cluster, index) {
return cluster.get().then(function(response) {
//some logic
});
});
return $q.all(promises);
}).then(function() {
// logic for when all of the get methods are complete
});
关于javascript - 使用 Angular.js 和 Restangular,如何在循环内的所有子请求完成时进行 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26267747/