使用 Angular 我们如何在完成一堆异步调用后进行调用
Using Angular how can we make a call after completing bunch of asynchronous calls
这是我在控制器中的代码
myFactory.getPoints().success(function (data) {
$scope.points = data;
});
myFactory.getStates().success(function (data) {
$scope.states = data;
});
myFactory.getLeases().success(function (data) {
$scope.leases = data;
});
我希望 $scope.leases 在 $scope.points 和 $scope.states 获得它们的值之后获得值。我了解到 $q 与 .then 不是同步的。是吗?
答案:我从答案中得出以下结论
$q.all([
myFactory.getPoints().success(function (data) {
$scope.Points = data;
}).error(function (data) {
$scope.error = "An Error has occurred while loading Points! " + data.ExceptionMessage;
}), //more, more, more
]).then(function () {
myFactory.getLease().success(function (data) {
$scope.leases = data;
}).error(function (data) {
$scope.error = "An Error has occurred while loading Leases! " + data.ExceptionMessage;
})
});
您可以将 promise 链接在一起,以便它们同步执行:
myFactory.getPoints().success(function (data) {
$scope.points = data;
return myFactory.getStates();
})
.success(function (data) {
$scope.states = data;
return myFactory.getLeases();
})
.success(function (data) {
$scope.leases = data;
});
问题是 success() 执行 return 最初的承诺 。 then()
调用 return 一个 promise(使用回调中 return 的值解决),而 .success()
是更传统的回调注册方式,return一个承诺。
所以一个合适的解决方案是
$q.all([
myFactory.getPoints().then(function (response) {
$scope.points = response.data;
return response.data;
}),
myFactory.getStates().then(function (response) {
$scope.states = response.data;
return response.data;
}),
myFactory.getLeases().then(function (response) {
$scope.leases = response.data;
return response.data;
})
]).then(function(responses) {
// xxx
});
这是我在控制器中的代码
myFactory.getPoints().success(function (data) {
$scope.points = data;
});
myFactory.getStates().success(function (data) {
$scope.states = data;
});
myFactory.getLeases().success(function (data) {
$scope.leases = data;
});
我希望 $scope.leases 在 $scope.points 和 $scope.states 获得它们的值之后获得值。我了解到 $q 与 .then 不是同步的。是吗?
答案:我从答案中得出以下结论
$q.all([
myFactory.getPoints().success(function (data) {
$scope.Points = data;
}).error(function (data) {
$scope.error = "An Error has occurred while loading Points! " + data.ExceptionMessage;
}), //more, more, more
]).then(function () {
myFactory.getLease().success(function (data) {
$scope.leases = data;
}).error(function (data) {
$scope.error = "An Error has occurred while loading Leases! " + data.ExceptionMessage;
})
});
您可以将 promise 链接在一起,以便它们同步执行:
myFactory.getPoints().success(function (data) {
$scope.points = data;
return myFactory.getStates();
})
.success(function (data) {
$scope.states = data;
return myFactory.getLeases();
})
.success(function (data) {
$scope.leases = data;
});
问题是 success() 执行 return 最初的承诺 。 then()
调用 return 一个 promise(使用回调中 return 的值解决),而 .success()
是更传统的回调注册方式,return一个承诺。
所以一个合适的解决方案是
$q.all([
myFactory.getPoints().then(function (response) {
$scope.points = response.data;
return response.data;
}),
myFactory.getStates().then(function (response) {
$scope.states = response.data;
return response.data;
}),
myFactory.getLeases().then(function (response) {
$scope.leases = response.data;
return response.data;
})
]).then(function(responses) {
// xxx
});