gpt4 book ai didi

javascript - AngularJS:依赖异步请求

转载 作者:行者123 更新时间:2023-11-28 07:39:31 27 4
gpt4 key购买 nike

AngularJS 新手,想知道处理三个异步请求的正确方法是什么,其中第三个请求的参数由前两个请求的响应定义。

two addresses are passed to this function
$scope.getRoutes = function(origin, destination) {

//1.A request is made to get the coordinates of the first address
dataFactory.geocode(origin)
.success(function(data) {
$scope.originObj = data;
})
.error(function () {
});

//2.Same for the second address
dataFactory.geocode(destination)
.success(function(data) {
$scope.destinationObj = data;
})
.error(function () {
});

//3.Finally a request for transport routes between the two sets of coordinates
dataFactory.getRoutes($scope.originObj.coordinates, $scope.destinationObj.coordinates)
.success(function(data) {
$scope.routes = data;
})
.error(function () {
});
};

这给了我错误:TypeError:无法读取未定义的属性“坐标”

将请求嵌套在成功函数中,但是有没有更好的方法让最后一个请求等待其他两个请求?

最佳答案

您正在寻找的是链式 promise 的能力。 “一旦收到来自此的数据,请调用此”是通用格式。 Angular 利用 $q 库。您可以找到文档here .

关于javascript - AngularJS:依赖异步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28227241/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com