【发布时间】:2015-07-24 22:13:56
【问题描述】:
我仍然在使用 Angular 承诺时遇到问题。我有三个工厂:getPosition,显然获取当前位置,geoCode,它使用谷歌的反向地理编码来获取城市名称,以及getData,它根据位置从 API 获取一些数据。我需要在获得职位后立即致电geoCode 和getData。我设法像这样链接getPosition 和geoCode:
getPosition.get().then(function(geoInfo){
$scope.$storage.geoInfo = geoInfo;
return geoCoding.getFromObj(geoInfo).then(function(city){
$scope.$storage.city = city;
$scope.city = $scope.$storage.city;
},function(reason){
alert("Geocoding error: " + reason);
})
}, function(reason){
alert("Location error: " + reason);
});
如何将getData 的调用添加到链中,以便使用第一次调用的数据调用它?我尝试过这样的事情:
getPosition.get().then(function(geoInfo){
$scope.$storage.geoInfo = geoInfo;
return function(geoInfo){
geoCoding.getFromObj(geoInfo).then(function(city){
$scope.$storage.city = city;
$scope.city = $scope.$storage.city;
},function(reason){
alert("Geocoding error: " + reason);
});
getData.getFromObj(geoInfo).then(function(data){
$scope.$storage.data=data;
})
}
}, function(reason){
alert("Location error: " + reason);
});
但是geoCoding 和getData 都不能这样工作。互联网上的大多数教程/文章也解释了如何一个接一个地链接调用,但我找不到任何解释如何像我需要的那样链接它们。
【问题讨论】:
标签: javascript ajax angularjs angular-promise