Javascript 在指令定义中返回对象与返回函数之间的区别?
以下代码(在Widget Uno中)使用指令定义对象(我认为它被称为..?)之间的功能区别是什么 …这段代码在小部件Dos中Javascript 在指令定义中返回对象与返回函数之间的区别?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,以下代码(在Widget Uno中)使用指令定义对象(我认为它被称为..?)之间的功能区别是什么 …这段代码在小部件Dos中 angular.module("app").directive('widgetDos', function($http) { return function(scope, element, attrs) { // A whole bunch of crap going on here }; }); 我试图将类似于Widget Uno的指令
angular.module("app").directive('widgetDos', function($http) {
return function(scope, element, attrs) {
// A whole bunch of crap going on here
};
});
我试图将类似于Widget Uno的指令转换为Widget Dos,但是我在哪里引用templateUrl呢?这在小部件Dos中可能吗?它应该是这样工作的:
angular.module("app").directive('widgetDos', function($http) {
return function(scope, element, attrs) {
// A whole bunch of crap going on here
};
});
angular.module("app").directive('widgetDos', function($http) {
return {
templateUrl: "....",
link: function(scope, element, attrs) {
// A whole bunch of crap going on here
};
}
});
另请参见(长版本)。有一个例子。仅返回指令中的函数只是完整定义中
链接
函数的简写
如果指定的不是链接
函数(如templateUrl
),则需要长时间编写:
angular.module("app").
directive("widgetUno", ["$http", function ($http) {
return {
link: function(scope, element, attrs) {
// A whole bunch of crap going on here
},
templateUrl: "widgetUno.html"
};
}]);
此差异实际上记录在此处-返回函数的实际上是以下各项的快捷方式:
angular.module("app").directive('widgetDos', function($http) {
return {
link: function(scope, element, attrs) {
//...
};
}
});
在指令不需要模板、控制器等的情况下使用它。除此之外,这两种调用方法在功能上完全没有区别