I am working on an apps to read JSON file.
Currently I am able to retrieve and read the JSON file but the problem is when to choose data in JSON.
Error "Cannot read property 'length' of undefined"
Below is my code.
Services.js
.factory("mainData", function($http, $log, $q) {
var chartData;
return {
all: function() {
var d = $q.defer();
$http({ method: 'GET',
url: 'http://localhost:8080/aZolla/getGlazCharts.do'})
.success(function (data, status, header, config) {
d.resolve(data);
})
.error(function (data, status, header, config) {
$log.warn(data, status, header, config);
});
chartData = d.promise;
return d.promise;
},
get: function(chartID) {
for (var i = 0; i < chartData.length; i++) {
if (chartData[i].id === parseInt(chartID)) {
return chartData[i];
}
}
return null;
}
};
});
Controller.js
.controller("ChartsListsCtrl", function($scope, mainData) {
mainData.all().then(function(data){
$scope.chartLists = data;
}
)
.controller('ChartsCtrl', function($scope, $stateParams, mainData) {
$scope.chart = mainData.get($stateParams.chartId);
})