AngularJS 指令中的 function () 与 function factory()
AngularJS function () vs function factory() in directive
在 angular 中,我看到指令都写成:
.directive('example', function () {
// Code
});
.directive('example', function factory() {
// Code
})
两者有什么区别?
function(){..}
是一个匿名函数。
function foo(){..}
是一个命名函数。
没有区别,除此之外,在功能上。命名函数更适合调试目的。
这只是传递一个命名函数而不是一个匿名函数。
你可以像下面这样写函数:
var foo = function() {
//function content
}
或
var foo = function foo() {
//function content
}
在 angular 中,我看到指令都写成:
.directive('example', function () {
// Code
});
.directive('example', function factory() {
// Code
})
两者有什么区别?
function(){..}
是一个匿名函数。
function foo(){..}
是一个命名函数。
没有区别,除此之外,在功能上。命名函数更适合调试目的。
这只是传递一个命名函数而不是一个匿名函数。
你可以像下面这样写函数:
var foo = function() {
//function content
}
或
var foo = function foo() {
//function content
}