// MODULE
var weatherApp = angular.module('weatherApp', ['ngRoute', 'ngResource']);
// Directives
weatherApp.service('cityService', function() {
this.city = "New York, NY";
});
//控制器
weatherApp.controller('homeController', ['$scope','cityService', function($scope, cityService){
$scope.city = cityService.city;
$scope.$watch('city', function(){
cityService.city = $scope.city;
});
}]);
weatherApp.controller('forecastController', ['$scope', 'cityService', '$resource', function($scope, $resource, cityService){
$scope.city = cityService.city;
$scope.weatherApi = $resource('http://api.openweathermap.org/data/2.5/forecast', {
callback: 'JSON_CALLBACK'
}, {get: { method: 'JSONP'}});
$scope.weatherResult = $scope.weatherApi.get({
q: $scope.city,
cnt: 2,
appid: 'b0a06997003bb34ff74635549a8bfd0e'
});
console.log($scope.weatherResult);
}]);
答案 0 :(得分:1)
根据文档https://openweathermap.org/current#name
,您不应传递州名,而只能传递城市名和可选的国家名。因此,http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20US
有效,而http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20NY
无效。有趣的是,http://api.openweathermap.org/data/2.5/forecast?appid=XXX&q=New%20York,%20NY,%20US
也可以使用,尽管看起来好像没有记录。