所以我终于让我的应用程序工作到它获得JSON请求的正确URL.但是现在我无法使用该URL.
我知道该服务正在从Google Maps API返回承诺,我可能不应该这样做,但如果我将其删除,我会收到"Weather.getWeather is undefined"错误.我不知道为什么.
我怎样才能让它正常工作.谢谢你的帮助.
weatherService.getWeather = function(city) {
var coordsUrl = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + city;
return $http.get(coordsUrl)
.success(function(data) {
var coords = data.results[0].geometry.location.lat + ',' + data.results[0].geometry.location.lng;
return getWeatherData(coords);
});
function getWeatherData(coords) {
var deferred = $q.defer(),
apiKey = 'cbbdddc644184a1d20ffc4a0e439650d',
weatherUrl = 'https://api.forecast.io/forecast/' + apiKey + '/' + coords + '?callback=JSON_CALLBACK';
$http.jsonp(weatherUrl)
.success(function(data) {
deferred.resolve(data);
}).error(function(err) {
deferred.reject(err);
});
console.log(weatherUrl);
return deferred.promise;
}
};
Run Code Online (Sandbox Code Playgroud)
控制器:
vm.fetchWeather = function(city) {
Weather.getWeather(city)
.then(function(data) {
console.log(data);
vm.place = data;
});
};
Run Code Online (Sandbox Code Playgroud)