I am facing a challenge with handling article information stored in a json file, where each article has a unique id. The format of the json data is as follows:
[{"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 displays the title and author of each article. When a user clicks on an article, I want to show detailed information such as the text, topic, etc. In order to retrieve the json data, I utilize the following AngularJS factory:
var trooNewsServices = angular.module('trooNewsServices', ['ngResource']);
trooNewsServices.factory('Articles', ['$resource',
function($resource){
return $resource('resources/articles.json');
}]);
To access the full list of articles in my home page controller, I use the following code:
this.articles = Articles.query();
My current struggle lies in displaying the selected article's information on the next page. I aim to achieve this by retrieving the specific id using $routeParams.id and then locating the corresponding article in the json data. Below is a snippet from 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);
}]);
However, I encounter an error stating 'Could not instantiate controller ArticleController'. Is there a way to access the selectedArticle variable outside the promise function?