How do I retrieve values returned from a service using a controller? In my code, the function showInfo() in service.js returns JSON objects. However, I am unable to access these objects outside of this function. When I attempt to use console.log in controller.js
console.log(chartService.showInfo.new_data)
I receive an
error stating Cannot read property 'new_data' of undefined.
The same issue occurs if I try
console.log(chartService.showInfo)
It returns undefined.
How can I access the new_data JSON object inside the showInfo function from the controller?
Service.js
angular.module('myApp')
.service('chartService', function (){
return {
getUrl: function init(path) {
Tabletop.init( { key: path,
callback: showInfo,
simpleSheet: true } )
}
}
function showInfo(data, tabletop){
var new_data = JSON.stringify(data.map(function(el) {
return {
"name": el[Object.keys(el)[0]],
"y": +el[Object.keys(el)[1]]
};
}));
}
})
Controller.js
angular.module('myApp')
.controller('piechartCtrl', [ '$scope', 'chartService', function (chartService, $scope) {
console.log(chartService.showInfo.new_data)
}]);