2016-02-18 4 views
0
weatherApp.controller('forecastController', ['$scope','weatherService','$resource','$log', function($scope,weatherService,$resource,$log){ 

var cnto =3; 
$scope.forecastholder = weatherService.holder; 
$scope.weatherAPI = $resource("http://api.openweathermap.org/data/2.5/forecast", 
           {callback: "JSON_CALLBACK"}, {get:{method:'JSONP'}}); 
$scope.weatherResult = $scope.weatherAPI 
.get({ 
    q: $scope.forecastholder, cnt: cnto, appid: "7acd7f3379f4ecc2cd8b5068f06e9ff1"}); 
}); 

$ scope.weatherResult.list дает мне неопределенное значение в консоли.Не удается получить доступ к элементу объекта в угловом

в то время как в html: {{weatherResult.list}} дает правильный результат. Как исправить эту проблему?

+1

'weatherResult' - выглядит это объект обещание .. вы, возможно, придется использовать функцию обратного вызова, чтобы получить фактический результат –

+0

Ресурс @ArunPJohny $ автоматически привяжет результаты запроса api так, как это написано. – bluetoft

+0

Видите ли ошибки в консоли разработчика при запуске этого кода? – bluetoft

ответ

0

Мне кажется, вам нужно добавить then(), чтобы вернуть данные.

$scope.weatherResult = $scope.weatherAPI 
.get({ 
    q: $scope.forecastholder, 
    cnt: cnto, 
    appid: "7acd7f3379f4ecc2cd8b5068f06e9ff1" 
}).then(function(response) { 
     $scope.weatherResult = response; 
     console.log($scope.weatherResult); 
}); 
+0

$ ресурс возвращает $ обещание, которое имеет. Then на нем. – bluetoft

0

Решение:

Использование $ promise.then (обратный вызов)

$scope.weatherResult = $scope.weatherAPI.get({ q: $scope.forecastholder, 
    cnt: cnto, appid: "7acd7f3379f4ecc2cd8b5068f06e9ff1"}).$promise.then(function(response) { 
    $scope.weatherResult = response; 
    console.log($scope.weatherResult.list[0].dt); 
});