返回指令定义中的对象和函数之间的区别
Differences between returning an object vs a function in a directive definition?
以下代码(在Widget Uno中)使用指令定义对象(我认为它被称为..?)…之间的功能区别是什么
angular.module("app").
directive("widgetUno", ["$http", function ($http) {
return {
// A whole bunch of crap going on here
},
templateUrl: "widgetUno.html"
};
}]);
Widget Dos中的这段代码呢?
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?这在Widget Dos中可能吗?
只返回指令中的函数只是完整定义中link
函数的简写。
如果您指定的是而不是link
函数(如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"
};
}]);
这种差异实际上记录在这里——http://docs.angularjs.org/guide/directive
返回函数的函数实际上是的快捷方式
angular.module("app").directive('widgetDos', function($http) {
return {
link: function(scope, element, attrs) {
//...
};
}
});
如果您的指令不需要模板、控制器等,请使用它。除此之外,这两种调用方法在功能上绝对没有区别。
它应该这样工作:
angular.module("app").directive('widgetDos', function($http) {
return {
templateUrl: "....",
link: function(scope, element, attrs) {
// A whole bunch of crap going on here
};
}
});
另请参阅http://docs.angularjs.org/guide/directive(长版)。有一个例子。
相关文章:
- Jquery在函数之间传递表行
- d3中堆栈函数和嵌套函数之间的差异
- 如何在函数之间切换
- JavaScript/jQuery-添加添加和删除类与下一个函数之间的延迟
- Javascript-defineProperty和直接在对象上定义函数之间的区别
- 在函数之间传递javascript变量
- 两个函数之间的角度承诺
- Javascript创建函数,以便在其他函数之间共享变量
- 直接绑定和使用每个函数之间的区别
- 节点模块对象范围:在所有函数之间共享一个对象
- js 函数之间使用参数的区别
- angularjs+ui.router:指令控制器和链接函数之间的不同行为
- 如何在onchange事件中的两个函数之间进行选择
- 在使用ajax调用的函数之间调用相同的函数
- 在JavaScript函数之间传递JSON值
- 试图理解JavaScript中原型和构造函数之间的区别
- 什么'新函数和函数之间的区别
- javascript函数之间的OR运算符
- 以下jquery和requireJS函数之间的区别
- 在Javascript中的函数之间共享变量