Javascript 无法读取angular.js控制器中的json响应属性
我正在使用名为“forecastController”的Angular.js控制器 Angular.js控制器代码为:Javascript 无法读取angular.js控制器中的json响应属性,javascript,angularjs,json,callback,controller,Javascript,Angularjs,Json,Callback,Controller,我正在使用名为“forecastController”的Angular.js控制器 Angular.js控制器代码为: weatherApp.controller('forecastController', ['$scope','$routeParams','cityService', 'weatherService', function($scope, $routeParams , cityService, weatherService) { $scope.city=
weatherApp.controller('forecastController',
['$scope','$routeParams','cityService', 'weatherService', function($scope, $routeParams , cityService, weatherService)
{
$scope.city=cityService.city;
$scope.days=$routeParams.days || '2' ;
$scope.weatherResult=weatherService.GetWeather($scope.city, $scope.days); //I get valid json response, the format is mentioned down below.
console.log($scope.weatherResult.city.name); //Not working, this is the problem
}]);
Json对象“$scope.weatherResult”是:
{
"city":
{
"id": 2643743,
"name": "London",
"coord": {
"lon": -0.12574,
"lat": 51.50853
},
"country": "GB",
"population": 0
},
"cod": "200"
}
我的服务是
weatherApp.service('weatherService', ['$resource',function($resource){
this.GetWeather=function(city, days){
var weatherAPI=$resource("http://api.openweathermap.org/data/2.5/forecast/daily?APPID={{MY_API_KEY_GOES_HERE}}",{
callback:"JSON_CALLBACK"}, {get:{method:"JSONP"}});
return weatherAPI.get({q:city, cnt:days});
};
}]);
我的“forecastController”收到有效的$scope.weatherResult。在HTML视图中,我可以访问weatherResult json对象属性。我已经妥善地保证了这一点。例如,{{weatherResult.city.name}可以工作。但是,如果我尝试在我的“forecaseController”中的console.log中打印,我会得到未定义的值。我得到的json数据来自
我得到的错误是:
TypeError: Cannot read property 'name' of undefined
at new <anonymous> (http://127.0.0.1:50926/controllers.js:19:42)
at e (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:215)
at Object.instantiate (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:344)
at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:72:460
at link (https://code.angularjs.org/1.3.0-rc.2/angular-route.min.js:7:268)
at Fc (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:68:47)
at K (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:57:259)
at g (https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:491)
at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:99
at https://code.angularjs.org/1.3.0-rc.2/angular.min.js:50:474 <div ng-view="" class="ng-scope">
TypeError:无法读取未定义的属性“name”
在纽约(http://127.0.0.1:50926/controllers.js:19:42)
在e(https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:215)
在Object.instantiate(https://code.angularjs.org/1.3.0-rc.2/angular.min.js:36:344)
在https://code.angularjs.org/1.3.0-rc.2/angular.min.js:72:460
在链接(https://code.angularjs.org/1.3.0-rc.2/angular-route.min.js:7:268)
在Fc(https://code.angularjs.org/1.3.0-rc.2/angular.min.js:68:47)
在K(https://code.angularjs.org/1.3.0-rc.2/angular.min.js:57:259)
在g(https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:491)
在https://code.angularjs.org/1.3.0-rc.2/angular.min.js:49:99
在https://code.angularjs.org/1.3.0-rc.2/angular.min.js:50:474
您的服务weatherService.GetWeather
可能会返回一个承诺。在解决该承诺之前,$scope.weatherResult
对象中的属性将是未定义的。使用承诺然后方法:
$scope.weatherResult = weatherService.GetWeather($scope.city, $scope.days);
$scope.weatherResult.$promise.then(function() {
console.log($scope.weatherResult.city.name);
});
更新:根据我的评论,该服务正在返回一个$resource
对象。此对象具有$promise
属性
嗨,科里,我试过你的建议了,它不起作用。我已经添加了服务代码,如果你能看一下,并建议我一些其他的。感谢您的帮助。它将返回一个$resource
对象,该对象具有$promise
属性。我已经更新了答案以包含$promise.then
方法。它未经测试…我按照你的第二个解决方案进行了尝试。仍然不起作用。同样的错误。注意到我的错误,把它分成两行。此外,还添加了一个带有工作示例的链接。