Angularjs for 循环承诺回调
Angularjs for loop promises callback
我想知道是否有可能在一系列请求结束时获得回调。我附上了示例代码,以便您更好地理解:
for (var i = 0; i < $scope.array.length; i++) {
factory.getX($scope.array[i].id)
.success(function (_response) {
/* call function if it's the last request in the for loop*/
})
.error(function (_err) {
$scope.displayAlert(_err)
})
}
在工厂对象中,我有一个 $http.get()
函数。谢谢!
如果你想在所有请求完成时有一个回调,你可以保存在数组中创建的所有承诺,并与 var allRequests = $q.all(arrayOfPromises)
共同承诺
然后您可以 allRequests.then(callback)
实现回调
谢谢大家,我已经解决了这个问题:
var promises = $scope.array.map(function (item) {
return factory.getX(item.id)
.success(function (_response) {
})
.error(function (_err) {
$scope.displayAlert(_err)
})
})
$q.all(promises).then(function () {
console.log("Solved all the promises")
}
我想知道是否有可能在一系列请求结束时获得回调。我附上了示例代码,以便您更好地理解:
for (var i = 0; i < $scope.array.length; i++) {
factory.getX($scope.array[i].id)
.success(function (_response) {
/* call function if it's the last request in the for loop*/
})
.error(function (_err) {
$scope.displayAlert(_err)
})
}
在工厂对象中,我有一个 $http.get()
函数。谢谢!
如果你想在所有请求完成时有一个回调,你可以保存在数组中创建的所有承诺,并与 var allRequests = $q.all(arrayOfPromises)
然后您可以 allRequests.then(callback)
实现回调
谢谢大家,我已经解决了这个问题:
var promises = $scope.array.map(function (item) {
return factory.getX(item.id)
.success(function (_response) {
})
.error(function (_err) {
$scope.displayAlert(_err)
})
})
$q.all(promises).then(function () {
console.log("Solved all the promises")
}