我有一个角度控制器,它使用工厂从 JSON 文件返回响应。当我尝试遍历返回的数据时,我在评论中看到以下错误。但是,如果我在尖括号内的 HTML 内使用 realArticleData 对象,它会完美地横向。我相信这与工厂返回的承诺有关,但我不确定。
控制器片段:
function SpecialOrderItemsCtrl($scope, $location, articleDataService) {
$scope.realArticleData = articleDataService.getArticles();
//This throws TypeError: Cannot read property 'thdCustomer' of undefined
$scope.custInfo = $scope.realArticleData.articles.thdCustomer;
}
HTML 片段:
<div ng-controller=SpecialOrderItemsCtrl>
<label>Raw Article JSON</label>
<p>{{realArticleData}}</p>
<label>Json transversed by one level</label>
<p>{{realArticleData.articles}}</p>
<label>THD CUstomer </label>
<p>{{realArticleData.articles.thdCustomer}}</p>
</div>
工厂:
function articleDataService($rootScope, $http) {
articleDataService.data = {};
articleDataService.getArticles = function() {
$http.get('articleData.json')
.success(function(data) {
articleDataService.data.articles = data;
});
return articleDataService.data;
};
return articleDataService;
}
sendDesign.factory('articleDataService', articleDataService);