I'm pretty new to Angular, but a recent problem I debugged in my application went as follows:
Say I'm attaching a new directive to an existing module defined in another .js file. When I use the following syntax:
angular
.module('myApp')
.directive('ratingStars', ratingStars)
;
var ratingStars = function(){
return {
restrict: 'EA',
scope: {thisRating : '=rating'},
templateUrl: '/my.template.html'
};
};
I would get an error, along the lines of "Argument 'fn' is not a function, got undefined".
However, I could fix the code using the following (note the use of function directly rather than assigning to a var).
angular
.module('myApp')
.directive('ratingStars', ratingStars)
;
function ratingStars(){
return {
restrict: 'EA',
scope: {thisRating : '=rating'},
templateUrl: '/my.template.html'
};
}
What is the logical difference between taking my function and assigning to a var, rather than defining the function outright? Is some sort of variable hoisting going on? Is my var only local to that one file?
Thanks much.