我有一个json文件,其中包含文章信息,其中每篇文章都有一个id。这是它是如何格式化:访问AngularJS中的特定json数据
[{"title":"ISIS No. 2 killed in US special ops raid",
"id":"14589192090024090",
"agency":"foxnews",
"text":"Article text goes here.",
"topic":"Politics",
"imagePath":"resources\/images\/foxnews\/14589192090024090.img.jpg"},
{"title":", etc etc }]
在我的主网页我有一个列表视图,显示标题和作者,当你点击文章中,我希望它显示文本,主题,等我angularJS工厂请求JSON看起来是这样的:
this.articles = Articles.query();
什么我挣扎的时候我点击一个特定的:
var trooNewsServices = angular.module('trooNewsServices', ['ngResource']);
trooNewsServices.factory('Articles', ['$resource',
function($resource){
return $resource('resources/articles.json');
}]);
我通过访问我的主页控制器的文章的完整列表无聊的文章我希望下一页显示那篇文章的信息。我试图通过使用$ routeParams.id获取特定的ID然后在json中查找id来做到这一点。这里是我选择的文章控制器中的代码:
TrooNews.controller('ArticleController', ['$routeParams','Articles',
function($routeParams, Articles) {
//look through articles for specific article using routeparams
Articles.query().$promise.then(function(articles) {
for(i = 0; i < articles.length; i++){
if(articles[i].id===$routeParams.id){
this.selectedArticle=articles[i];
}
}
});
console.log(selectedArticle);
}]);
我收到一个错误'无法实例化控制器ArticleController'。如何在promise函数外部访问我的selectedArticle变量?
您的模块名称是** trooNewsServices **。使用** trooNewsServices.controller('ArticleController',['$ routeParams','Articles','$ scope', function($ routeParams,Articles,$ scope){.....})** –
主要TrooNews模块在一个单独的文件我包括trooNewsServices作为该项目的依赖项,所以不应该是问题 –
你可以提供我详细的代码。会给你Plunker。 –