I've been trying for a while and looked through several answers, I can't figure out why this is not working:
I need to share some data between controllers, so I set up a service right? (The data is obtained from a file, I'm using node-webkit)
.service('tagList', function() {
this.getTags = function() {
var t;
fs.readFile('tags', 'utf8', function(err, data) {
if (err) throw err;
console.debug(data.split(','));
t = data.split(',');
});
console.debug(t);
return t;
};
})
Then in some controller I'd do
.controller('sidebarCtrl', function($scope, tagList) {
$scope.tags = tagList.getTags();
})
But tags ends up as undefined
, the console.debug
inside readFile
both show t how it should be.
But the console.debug
outside readFile
, shows it as undefined
, why? If it is declared on getTags
scope.