我正在关注使用 $resource 从 API 调用中检索 JSON 数据的 AngularJS 教程。为了便于理解,我尝试将 $resource 代码替换为 $http 代码,但遇到了作用域问题。在结果$scope.weatherResult
之外登录。为什么会这样?视图接收数据就好了。.success()
undefined
还,
// $scope.weatherAPI = $resource(
'http://api.openweathermap.org/data/2.5/forecast/daily',
{ callback: 'JSON_CALLBACK' }, { get: { method: 'JSONP' }}
);
// $scope.weatherResult = $scope.weatherAPI.get({ q: $scope.city, cnt: 2});
$http.get('
http://api.openweathermap.org/data/2.5/forecast/daily'
+ '?q='
+ $scope.city
+ '&'
+ 'cnt=2'
)
.success(function(data) {
$scope.weatherResult = data;
})
.error(function(error) {
console.log(error);
});
console.log($scope.weatherResult);