我对Angular很新,但我在我的应用程序中调试的最近一个问题如下:
假设我正在将新指令附加到另一个.js文件中定义的现有模块.当我使用以下语法时:
angular
.module('myApp')
.directive('ratingStars', ratingStars)
;
var ratingStars = function(){
return {
restrict: 'EA',
scope: {thisRating : '=rating'},
templateUrl: '/my.template.html'
};
};
Run Code Online (Sandbox Code Playgroud)
我会得到一个错误,沿着"Argument'fn'不是一个函数,未定义".
但是,我可以使用以下代码修复代码(注意直接使用函数而不是分配给var).
angular
.module('myApp')
.directive('ratingStars', ratingStars)
;
function ratingStars(){
return {
restrict: 'EA',
scope: {thisRating : '=rating'},
templateUrl: '/my.template.html'
};
}
Run Code Online (Sandbox Code Playgroud)
获取函数和赋值给var之间的逻辑区别是什么,而不是直接定义函数?是否有某种变量悬挂?我的var只是那个文件的本地吗?
非常感谢.