Hi. I’m having issues loading a json file and store the data in controller. I searched for similar problems, but i could not find anything (maybe because i was not using the correct search criteria).
services.js
…
.factory(“Subscriptions”, [’$http’, function($http) {
var subscriptions = [];
$http.get('subscriptions.json').success(function(data) {
subscriptions = data;
});
return {
all: function() {
return subscriptions;
},
get: function(subscriptionId) {
for (var i = 0; i < subscriptions.length; i++) {
if (subscriptions[i].subscriptionId === parseInt(subscriptionId)) {
return subscriptions[i];
}
}
return null;
}
};
}])
…
controllers.js
…
.controller(“SubscriptionsCtrl”, ["$scope", “Subscriptions”, function($scope, Subscriptions) {
$scope.subscriptions = Subscriptions.all();
}])
The problem is, when i go to the page which lists all subscriptions, it all appears blank. I tried to change the code a bit, and that worked, but i did not liked the solution, because i think that’s not the best practice.
$http.get('subscriptions.json').success(function(data) {
for(var i = 0; i < data.length; i++) {
subscriptions.push(data[i]);
}
});
Can you guys help me please?
Note: btw, it would be usefull if ionic would include an example with json file load, instead of hardcoded data.