[英]Accessing specific json data in AngularJS
I have a json file that includes article information where each article has an id.我有一个包含文章信息的 json 文件,其中每篇文章都有一个 id。 This is how it is formatted:这是它的格式:
[{"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 }]
On my main page I have a list view that shows the title and author and when you click on the article I want it to display the text, topic, etc. My angularJS factory for requesting the json looks like this:在我的主页上,我有一个列表视图,显示标题和作者,当您单击文章时,我希望它显示文本、主题等。我用于请求 json 的 angularJS 工厂如下所示:
var trooNewsServices = angular.module('trooNewsServices', ['ngResource']);
trooNewsServices.factory('Articles', ['$resource',
function($resource){
return $resource('resources/articles.json');
}]);
I access the full list of articles in my home page controller by using:我使用以下方法访问主页控制器中的完整文章列表:
this.articles = Articles.query();
What I am struggling with is when I click on a specific article I want that next page to display that articles info.我正在苦苦挣扎的是,当我单击特定文章时,我希望下一页显示该文章信息。 I am attempting to do this by getting the specific id using $routeParams.id and then looking for the id in the json.我试图通过使用 $routeParams.id 获取特定 id 然后在 json 中查找 id 来做到这一点。 Here is the code in my selected article controller:这是我选择的文章控制器中的代码:
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);
}]);
I get an error 'Could not instantiate controller ArticleController'.我收到错误“无法实例化控制器 ArticleController”。 How can I access my selectedArticle variable outside the promise function?如何在 promise 函数之外访问我的 selectedArticle 变量?
TrooNews.controller('ArticleController', ['$routeParams','Articles',
function($routeParams, Articles) {
var parent = this;
parent.selectedArticle = null;
parent.setSelectedArticle = function(article){
parent.selectedArticle = article;
}
//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){
parent.setSelectedArticle(articles[i])
break;
}
}
});
}]);
You're making an async call, so don't expect the results to be there right after you call it.您正在进行异步调用,因此不要指望在调用后立即得到结果。 Use the callback method:使用回调方法:
Articles.query().then(function(articles){
for(i = 0; i < articles.length; i++){
if(article[i].id===id){
selectedArticle=article;
}
}
console.log(selectedArticle);
})
Or you can just force a scope digest inside the callback call.或者您可以在回调调用中强制使用范围摘要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.