Izh*_*aki 451 angularjs angularjs-directive
在编写Angular指令时,可以使用以下任何函数来操作声明指令的元素的DOM行为,内容和外观:
对于应该使用哪种功能似乎存在一些混淆.这个问题包括:
Izh*_*aki 167
基于以下plunk,请考虑以下HTML标记:
<body>
<div log='some-div'></div>
</body>
Run Code Online (Sandbox Code Playgroud)
使用以下指令声明:
myApp.directive('log', function() {
return {
controller: function( $scope, $element, $attrs, $transclude ) {
console.log( $attrs.log + ' (controller)' );
},
compile: function compile( tElement, tAttributes ) {
console.log( tAttributes.log + ' (compile)' );
return {
pre: function preLink( scope, element, attributes ) {
console.log( attributes.log + ' (pre-link)' );
},
post: function postLink( scope, element, attributes ) {
console.log( attributes.log + ' (post-link)' );
}
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
控制台输出将是:
some-div (compile)
some-div (controller)
some-div (pre-link)
some-div (post-link)
Run Code Online (Sandbox Code Playgroud)
我们可以看到,compile
在执行第一次,然后controller
,再pre-link
和最后的post-link
.
注意:以下内容不适用于在其链接函数中呈现其子项的指令.相当多的Angular指令都这样做(比如ngIf,ngRepeat或任何指令
transclude
).这些指令本身会在link
调用其子指令之前调用它们的函数compile
.
原始HTML标记通常由嵌套元素组成,每个元素都有自己的指令.如下面的标记(参见plunk):
<body>
<div log='parent'>
<div log='..first-child'></div>
<div log='..second-child'></div>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
控制台输出如下所示:
// The compile phase
parent (compile)
..first-child (compile)
..second-child (compile)
// The link phase
parent (controller)
parent (pre-link)
..first-child (controller)
..first-child (pre-link)
..first-child (post-link)
..second-child (controller)
..second-child (pre-link)
..second-child (post-link)
parent (post-link)
Run Code Online (Sandbox Code Playgroud)
我们可以在这里区分两个阶段 - 编译阶段和链接阶段.
当加载DOM时,Angular启动编译阶段,从而自上而下遍历标记,并调用compile
所有指令.从图形上看,我们可以这样表达:
或许重要的是要提到,在这个阶段,编译函数获取的模板是源模板(而不是实例模板).
DOM实例通常只是将源模板呈现给DOM的结果,但它们可以由动态创建ng-repeat
或引入.
每当具有指令的元素的新实例被呈现给DOM时,链接阶段就开始了.
在这个阶段,Angular调用controller
,pre-link
迭代子节点,并调用post-link
所有指令,如下所示:
Izh*_*aki 90
各种指令函数从内调用其他两个角函数的,执行$compile
(其中指令的compile
执行)和称为内部函数nodeLinkFn
(其中,该指令的controller
,preLink
与postLink
被执行).在调用指令函数之前和之后,角函数内发生了各种各样的事情.也许最值得注意的是儿童递归.以下简化图显示了编译和链接阶段的关键步骤:
为了演示这些步骤,我们使用以下HTML标记:
<div ng-repeat="i in [0,1,2]">
<my-element>
<div>Inner content</div>
</my-element>
</div>
Run Code Online (Sandbox Code Playgroud)
使用以下指令:
myApp.directive( 'myElement', function() {
return {
restrict: 'EA',
transclude: true,
template: '<div>{{label}}<div ng-transclude></div></div>'
}
});
Run Code Online (Sandbox Code Playgroud)
该compile
API看起来就像这样:
compile: function compile( tElement, tAttributes ) { ... }
Run Code Online (Sandbox Code Playgroud)
通常,参数的前缀t
是表示提供的元素和属性是源模板的元素,而不是实例的元素和属性.
在compile
删除对已转换内容(如果有)的调用之前,将模板应用于标记.因此,提供给compile
函数的元素将如下所示:
<my-element>
<div>
"{{label}}"
<div ng-transclude></div>
</div>
</my-element>
Run Code Online (Sandbox Code Playgroud)
请注意,此时不会重新插入已转换的内容.
在调用指令之后.compile
,Angular将遍历所有子元素,包括那些可能刚刚被指令引入的元素(例如,模板元素).
在我们的例子中,将创建上面的三个源模板实例(by ng-repeat
).因此,以下序列将执行三次,每个实例一次.
该controller
API包括:
controller: function( $scope, $element, $attrs, $transclude ) { ... }
Run Code Online (Sandbox Code Playgroud)
进入链接阶段,$compile
现在返回的链接函数提供了一个范围.
首先,如果请求,链接函数创建子范围(scope: true
)或隔离范围(scope: {...}
).
然后执行控制器,提供实例元素的范围.
该pre-link
API看起来就像这样:
function preLink( scope, element, attributes, controller ) { ... }
Run Code Online (Sandbox Code Playgroud)
实际上,在对指令的调用.controller
和.preLink
函数之间没有任何反应.Angular仍然建议如何使用每个.
在.preLink
调用之后,链接函数将遍历每个子元素 - 调用正确的链接函数并将当前作用域(作为子元素的父作用域)附加到其上.
post-link
API类似于pre-link
函数的API :
function postLink( scope, element, attributes, controller ) { ... }
Run Code Online (Sandbox Code Playgroud)
也许值得注意的是,一旦.postLink
调用了一个指令的函数,它的所有子元素的链接过程就完成了,包括所有.postLink
子函数.
这意味着,在.postLink
被召唤的时候,孩子们"活着"就准备好了.这包括:
因此,此阶段的模板将如下所示:
<my-element>
<div class="ng-binding">
"{{label}}"
<div ng-transclude>
<div class="ng-scope">Inner content</div>
</div>
</div>
</my-element>
Run Code Online (Sandbox Code Playgroud)
Izh*_*aki 43
如果要使用全部四个函数,该指令将遵循以下形式:
myApp.directive( 'myDirective', function () {
return {
restrict: 'EA',
controller: function( $scope, $element, $attrs, $transclude ) {
// Controller code goes here.
},
compile: function compile( tElement, tAttributes, transcludeFn ) {
// Compile code goes here.
return {
pre: function preLink( scope, element, attributes, controller, transcludeFn ) {
// Pre-link code goes here
},
post: function postLink( scope, element, attributes, controller, transcludeFn ) {
// Post-link code goes here
}
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
请注意,compile返回一个包含pre-link和post-link函数的对象; 在Angular lingo中我们说compile函数返回一个模板函数.
如果pre-link
没有必要,编译函数可以简单地返回post-link函数而不是定义对象,如下所示:
myApp.directive( 'myDirective', function () {
return {
restrict: 'EA',
controller: function( $scope, $element, $attrs, $transclude ) {
// Controller code goes here.
},
compile: function compile( tElement, tAttributes, transcludeFn ) {
// Compile code goes here.
return function postLink( scope, element, attributes, controller, transcludeFn ) {
// Post-link code goes here
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
有时,人们希望在定义compile
(post)link
方法后添加方法.为此,可以使用:
myApp.directive( 'myDirective', function () {
return {
restrict: 'EA',
controller: function( $scope, $element, $attrs, $transclude ) {
// Controller code goes here.
},
compile: function compile( tElement, tAttributes, transcludeFn ) {
// Compile code goes here.
return this.link;
},
link: function( scope, element, attributes, controller, transcludeFn ) {
// Post-link code goes here
}
};
});
Run Code Online (Sandbox Code Playgroud)
如果不需要编译函数,可以完全跳过它的声明,并link
在指令的配置对象的属性下提供post-link函数:
myApp.directive( 'myDirective', function () {
return {
restrict: 'EA',
controller: function( $scope, $element, $attrs, $transclude ) {
// Controller code goes here.
},
link: function postLink( scope, element, attributes, controller, transcludeFn ) {
// Post-link code goes here
},
};
});
Run Code Online (Sandbox Code Playgroud)
在上面的任何示例中,controller
如果不需要,可以简单地移除该功能.因此,例如,如果只post-link
需要功能,可以使用:
myApp.directive( 'myDirective', function () {
return {
restrict: 'EA',
link: function postLink( scope, element, attributes, controller, transcludeFn ) {
// Post-link code goes here
},
};
});
Run Code Online (Sandbox Code Playgroud)
Izh*_*aki 31
Angular允许DOM操作的事实意味着编译过程中的输入标记有时与输出不同.特别地,一些输入标记可以ng-repeat
在被渲染到DOM之前被克隆几次(例如).
角度术语有点不一致,但它仍然区分两种类型的标记:
以下标记演示了这一点:
<div ng-repeat="i in [0,1,2]">
<my-directive>{{i}}</my-directive>
</div>
Run Code Online (Sandbox Code Playgroud)
源html定义
<my-directive>{{i}}</my-directive>
Run Code Online (Sandbox Code Playgroud)
它充当源模板.
但由于它包含在ng-repeat
指令中,因此将克隆此源模板(在我们的示例中为3次).这些克隆是实例模板,每个都将出现在DOM中并绑定到相关范围.
Izh*_*aki 23
compile
当Angular bootstraps时,每个指令的函数只被调用一次.
正式地说,这是执行(源)模板操作的地方,不涉及范围或数据绑定.
首先,这是为了优化目的; 考虑以下标记:
<tr ng-repeat="raw in raws">
<my-raw></my-raw>
</tr>
Run Code Online (Sandbox Code Playgroud)
该<my-raw>
指令将呈现一组特定的DOM标记.所以我们可以:
ng-repeat
复制源模板(<my-raw>
),然后修改每个实例模板的标记(在compile
函数外部).compile
函数中),然后允许ng-repeat
复制它.如果raws
集合中有1000个项目,则后一个选项可能比前一个更快.
Izh*_*aki 20
controller
每当实例化新的相关元素时,都会调用每个指令的函数.
正式来说,controller
功能在于:
同样,重要的是要记住,如果指令涉及隔离范围,则其中继承自父范围的任何属性都不可用.
Izh*_*aki 19
post-link
调用该函数时,所有先前的步骤都已发生 - 绑定,转换等.
这通常是进一步操纵渲染DOM的地方.
Izh*_*aki 15
pre-link
每当实例化新的相关元素时,都会调用每个指令的函数.
正如之前在编译顺序部分中看到的那样,pre-link
函数被称为parent-then-child,而post-link
函数被调用child-then-parent
.
该pre-link
功能很少使用,但在特殊情况下可能很有用; 例如,当子控制器向父控制器注册自己时,注册必须是一种parent-then-child
方式(以ngModelController
这种方式做事).
归档时间: |
|
查看次数: |
149187 次 |
最近记录: |