$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);