Javascript AngularJS:依赖异步请求

Javascript AngularJS:依赖异步请求,javascript,angularjs,Javascript,Angularjs,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

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:无法读取未定义的属性“坐标”


将请求嵌套在success函数中,但是有没有更好的方法让最后一个请求等待另外两个呢

你要寻找的是能够连锁承诺的能力。“一旦你从这一个收到数据,调用这一个”是通用格式。Angular利用了$q库。您可以找到文档