【发布时间】:2015-11-27 10:51:10
【问题描述】:
我有一个简单的服务,可以获取一些 openWeather JSON。然后将服务注入控制器。在控制器中,我无法使用 JSON 访问对象,但是,当绑定到时,我可以毫无问题地访问对象数据。我是 JS 和 Angular 的新手,我做错了什么?
服务
mcmdApp.service('areaWeatherService', ['$resource', function($resource){
this.weatherAPI = $resource("http://api.openweathermap.org/data/2.5/weather", {get: {method: "JSON"}});
this.weatherResult = this.weatherAPI.get({q: "London,uk"});
}]);
控制器
mcmdApp.controller('SingleElementController',['$scope', 'areaWeatherService',function($scope, areaWeatherService){
$scope.weatherResult = areaWeatherService.weatherResult;
console.log($scope.weatherResult); //<-- Shows Object in Console
console.log($scope.weatherResult.wind); //<-- PROBLEM: Shows Undefined
console.log($scope.weatherResult.wind.speed); //<-- PROBLEM: Shows cannot read property 'speed' of Undefined
}]);
查看
<div class="single-element-widget text-center">
<h2>{{weatherResult.wind.speed}} mph</h2><!-- NO PROBLEM, displays correctly -->
<small>{{weatherResult.wind.deg}} degrees</small><!-- NO PROBLEM, displays correctly -->
</div>
我正在尝试从控制器或服务访问对象和属性。
console.log($scope.weatherResult);的结果
e {$promise: d, $resolved: false}$promise: d$resolved: truebase: "cmc stations"clouds: Objectcod: 200coord: Objectdt: 1441164423id: 2643743main: Objectname: "London"sys: Objectweather: Array[1]wind: Object__proto__: e
【问题讨论】:
-
发布console.log($scope.weatherResult)的结果;
-
编辑:我已经添加了
console.log($scope.weatherResult);的结果
标签: javascript json angularjs model-view-controller