无法以角度访问对象元素

时间:2016-02-18 03:04:33

标签: javascript angularjs

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}} 它给出了正确的结果。 如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我认为您需要添加then()才能返回数据。

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

答案 1 :(得分: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);
});