【发布时间】:2015-02-16 23:19:24
【问题描述】:
我正在尝试从控制器访问嵌套承诺的值。
这是我的控制器。我正在调用我的服务,希望返回一个城市名称:
LocationService.getCurrentCity(function(response) {
// This is never executed
console.log('City name retrieved');
console.log(response);
});
这里是服务。我正在更新客户的位置,然后我向 Google 请求城市。 console.log(city) 确实按预期记录正确的城市。
this.getCurrentCity = function() {
return this.updateMyPosition().then(function() {
return $http.get('http://maps.googleapis.com/maps/api/geocode/json?latlng=' + myPosition.lat + ','+ myPosition.lng +'&sensor=false').then(function(response) {
var city = response.data['results'][0]['address_components'][3]['long_name'];
console.log(city);
return city;
});
});
}
如何在我的控制器中访问city?
【问题讨论】:
标签: javascript angularjs location promise angular-promise