I’m a total mobile development/ionic newb. I’ve set up a project with a factory, and as long as I have the data hard-coded into the variable, it works. The problem is when I try to link to an external JSON file. I’m sure I have one little piece of syntax off, I’m just not seeing the problem. I’m pretty sure the JSON file is formatted correctly, because when I use my browser’s Inspector, I can see that the code is loading correctly, it’s just not printing on the screen.
Code below:
JAVASCRIPT:
angular.module('starter.services', [])
.factory('ProjectRecords', function($http) {
var projects = [];
$http.get("js/data.js").success(function(data){
this.projects = data;
});
return {
all: function() {
return projects;
},
get: function(projectId) {
for (var i = 0; i < projects.length; i++) {
if (projects[i].id === parseInt(projectId)) {
return projects[i];
}
}
return null;
}
};
});
controllers.js:
.controller('ViewProjectsCtrl', function($scope, $http, ProjectRecords) {
$scope.projects = ProjectRecords.all();
$scope.remove = function(projects) {
ProjectRecords.remove(projects);
};
})
.controller('ViewProjectDetailsCtrl', function($scope, $stateParams, $http, ProjectRecords) {
$scope.projectDetails = ProjectRecords.get($stateParams.projectId);
})
js/data.js:
{
"records":
[
{
"id": 1,
"RecordID": "XXXX",
"ProjectName": "Demo 1",
"Activity": "Fake Event 1",
"Date": "08/25/2015",
"Mileage": "100 mi",
"CEHours": "1.00",
"VolHours": "10.00"
},
{
"id": 2,
"RecordID": "XXXXXX",
"ProjectName": "Demo 2",
"Activity": "Fake Event 2",
"Date": "09/16/2015",
"Mileage": "2 mi",
"CEHours": "0.00",
"VolHours": "10.00"
},
{
"id": 3,
"RecordID": "XXXXXX",
"ProjectName": "Demo 3",
"Activity": "Fake Event 3",
"Date": "12/17/2014",
"Mileage": "5 mi",
"CEHours": "0.00",
"VolHours": "10.00"
}
]
}
What am I doing wrong???