I have an Ionic app where I am calling $http through a factory:
.factory('Articles', function ($http) {
var articles = [];
return {
all: function () {
return $http.get("http://jsonp.afeld.me/?url=http://examplesite.com/page.html?format=json").then(function (response) {
articles = response.data.items;
console.log(response.data.items);
return articles;
});
},
get: function (articleId) {
for (var i = 0; i < articles.length; i++) {
if (articles[i].id === parseInt(articleId)) {
return articles[i];
}
}
return null;
}
}
});
It is working fine and displays a list of articles but when I want to go into a specific article it just shows a blank page even though the url shows the correct article ID. My controller for a single article looks like this:
.controller('MyCtrl', function ($scope, $stateParams, Articles) {
$scope.article = Articles.get($stateParams.articleId);
})
The returned data looks like this (The ID seems to be coming through as a string but using "parseInt" so really not sure what is going on):
0: Object
alias: "title"
attachments: Array[0]
author: Object
category: Object
catid: "67"
created: "2015-04-11 08:06:07"
created_by_alias: ""
events: Object
extra_fields: null
featured: "0"
fulltext: ""
gallery: null
hits: "80"
id: "171"
image: ""
imageLarge: "/url/69bc9c3e85c501b0a6208cc7a55abbf9_L.jpg"
imageMedium: "/url/69bc9c3e85c501b0a6208cc7a55abbf9_M.jpg"
imageSmall: "/url/69bc9c3e85c501b0a6208cc7a55abbf9_S.jpg"
imageWidth: ""
imageXLarge: "/url/69bc9c3e85c501b0a6208cc7a55abbf9_XL.jpg"
imageXSmall: "/url/69bc9c3e85c501b0a6208cc7a55abbf9_XS.jpg"
image_caption: ""
image_credits: ""
introtext: "<p><strong>Mpho Mathoho</strong>lots of text is here....</p>"
language: "*"
link: "/url/title.html"
modified: "2015-06-02 07:44:30"
numOfComments: "0"
numOfvotes: "(0 votes)"
tags: Array[1]
title: "the title"
video: null
video_caption: ""
video_credits: ""
votingPercentage: 0
__proto__: Object
I guess you need to use parseInt()
on both fields, change the line:
if (articles[i].id === parseInt(articleId)) {
To:
if (parseInt(articles[i].id) === parseInt(articleId)) {