Angularjs 什么是module.directive函数参数?
我见过有很多不同函数参数的指令。到目前为止,我看到的情况如下: 空函数:Angularjs 什么是module.directive函数参数?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我见过有很多不同函数参数的指令。到目前为止,我看到的情况如下: 空函数: .directive('myDialog', function() { /* ... */ }); 数组: .directive('myDraggable', ['$document', function($document) { /* ... */ }); 还有一些: .directive('tetris', function ($timeout) { /* ... */ }); .directive('mypopo
.directive('myDialog', function() { /* ... */ });
数组:
.directive('myDraggable', ['$document', function($document) { /* ... */ });
还有一些:
.directive('tetris', function ($timeout) { /* ... */ });
.directive('mypopover', function ($compile,$templateCache) { /* ... */ });
.directive("myDirective", function($document) { /* ... */ });
我知道这只是不同的变量名,但它似乎用于不同的行为。
指令函数是如何工作的?这不仅仅是针对指令的,而是如何工作的语法。您可以按任何顺序向回调函数注入您希望在组件中使用的任何依赖项。在上面,您已经发布了两个不同版本的语法——一个带有数组,另一个不带数组
.directive("tetris", function ($timeout) {
这将自动向该指令注入$timeout
服务,以便在该指令的定义和功能中使用
.directive('myDraggable', ['$document', function($document) {
这是一种不同的语法编写方法,因此您可以根据需要调用$document
变量,这对于简化非常有用
依赖注入是AngularJS的杀手级功能,学习和理解它非常重要。指令应该用于DOM操作,简单明了。