I have declared a function in file so that it becomes global:
function speakService() {
var speakService = {};
var speak = function(word) {
console.log(word);
};
speakService.speak = speak;
return speakService;
}
Using AngularJS, I want to add this service as dependency:
angular
.module('login', ['ngRoute'])
.factory('speakService', [function() {
var speakService = speakService();
return speakService;
}]);
But as soon as the interpreter hits the line:
var speakService = speakService();
the speakService()-function is undefined. If I change the speakService variable to speakSvc like this it works fine:
var speakSvc = speakService();
Can somebody explain why my global function is undefined when declaring a local variable with the same name?
Regards!
speakServicefunction. Just return an object literal with an assigned speak function.