问题描述
从angularjs的文档,定义一个指令的时候,有一个 postLink
在编译
和 postLink
在链接
From the doc of angularjs, when defining a directive, there's a postLink
in compile
, and a postLink
in link
myModule.directive('directiveName', function factory(injectables) {
var directiveDefinitionObject = {
priority: 0,
template: '<div></div>',
templateUrl: 'directive.html',
replace: false,
transclude: false,
restrict: 'A',
scope: false,
compile: function compile(tElement, tAttrs, transclude) {
return {
pre: function preLink(scope, iElement, iAttrs, controller) { ... },
post: function postLink(scope, iElement, iAttrs, controller) { ... }
}
},
link: function postLink(scope, iElement, iAttrs) { ... }
};
return directiveDefinitionObject;
});
有什么它们之间的区别?我注意到 postLink
在链接
有一个说法一个比编译少
。以及是否有任何其他的区别吗?
What's the difference between them? I notice the postLink
in link
has a argument less than the one in compile
. And are there any other difference?
推荐答案
他们没有什么不同,你所拥有的有来自文档只是psuedo- code。该postLink函数是最重要的,所以有多种方式来声明。
They're no different, what you have there is just psuedo-code from the documentation. The postLink function is just the most important one, so there are a variety of ways to declare it.
下面是一个的作为一个例子...
Here is a Plunker as an example...
...这里是表示postLink功能的不同声明一些伪code:
... and here is some psuedo code showing the different declarations of a postLink function:
app.directive('dir1', function () {
return function(scope, elem, attr) {
//this is the same
};
});
app.directive('dir2', function () {
return {
link: function(scope, elem, attr) {
//this is the same
}
};
});
app.directive('dir3', function () {
return {
compile: function compile(tElement, tAttrs, transclude) {
return {
post: function postLink(scope, elem, attrs) {
//this is the same
}
}
}
};
});
...你只需要一个。
... you only need one.
这篇关于什么是2英寸之间的差; postLink&QUOT;在指令的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!