3 回答
TA贡献1818条经验 获得超8个赞
编译:
这是Angular实际编译指令的阶段。对于给定指令的每个引用,只调用一次此编译函数。例如,假设您正在使用ng-repeat指令。ng-repeat必须查找它所附加的元素,提取它所附加的html片段并创建模板函数。
如果您使用过HandleBars,下划线模板或等效模板,就像编译模板以提取模板函数一样。对于此模板函数,您传递数据,该函数的返回值是html,数据位于正确的位置。
编译阶段是Angular中返回模板函数的步骤。角度中的此模板函数称为链接函数。
链接阶段:
链接阶段是将数据($ scope)附加到链接函数的位置,它应该返回链接的html。由于该指令还指定了这个html的去向或更改的内容,因此它已经很好了。这是您要对链接的html进行更改的功能,即已经附加了数据的html。在角度中如果你在链接函数中编写代码,它通常是post-link函数(默认情况下)。它是一种在链接函数将数据与模板链接后调用的回调。
控制器:
控制器是您放置某些指令特定逻辑的地方。这个逻辑也可以进入链接函数,但是你必须将该逻辑放在范围上以使其“可共享”。问题在于,您将使用指令来破坏范围,而这些东西实际上并不是预期的。那么,如果两个指令希望彼此交谈/相互合作,那么又有什么选择呢?当然,您可以将所有逻辑放入服务中,然后使这两个指令依赖于该服务,但这只会带来一个依赖性。另一种方法是为这个范围提供一个控制器(通常是隔离范围?),然后当该指令“需要”另一个指令时,该控制器被注入另一个指令。
TA贡献1799条经验 获得超9个赞
A directive允许您以声明方式扩展HTML词汇表以构建Web组件。该ng-app属性是一个指令,所以是ng-controller所有的ng- prefixed attributes。指令可以是attributes,tags甚至class names是comments。
指令如何诞生(compilation和instantiation)
编译:我们将在呈现之前将该compile函数用于manipulateDOM并返回一个link函数(将为我们处理链接)。这也是放置任何需要与所有instancesthis指令共享的方法的地方。
link:我们将使用该link函数注册特定DOM元素上的所有侦听器(从模板克隆)并设置我们对页面的绑定。
如果设置在compile()函数中,它们只会被设置一次(这通常是你想要的)。如果在link()函数中设置,则每次将HTML元素绑定到对象中的数据时都会设置它们。
<div ng-repeat="i in [0,1,2]">
<simple>
<div>Inner content</div>
</simple></div>app.directive("simple", function(){
return {
restrict: "EA",
transclude:true,
template:"<div>{{label}}<div ng-transclude></div></div>",
compile: function(element, attributes){
return {
pre: function(scope, element, attributes, controller, transcludeFn){
},
post: function(scope, element, attributes, controller, transcludeFn){
}
}
},
controller: function($scope){
}
};
});Compile函数返回pre和post链接函数。在预链接函数中,我们有实例模板以及范围controller,但是模板没有绑定到范围,仍然没有被转换的内容。
Postlink function是post link是最后一个执行的函数。现在transclusion已经完成了the template is linked to a scope,和view will update with data bound values after the next digest cycle。该link选项只是设置post-link功能的快捷方式。
controller:指令控制器可以传递给另一个指令链接/编译阶段。它可以作为在指令间通信中使用的手段注入其他指南。
您必须指定所需指令的名称 - 它应绑定到同一元素或其父元素。该名称可以带有以下前缀:
? – Will not raise any error if a mentioned directive does not exist.^ – Will look for the directive on parent elements, if not available on the same element.
使用方括号[‘directive1′, ‘directive2′, ‘directive3′]需要多个指令控制器。
var app = angular.module('app', []);app.controller('MainCtrl', function($scope, $element) {});app.directive('parentDirective', function() {
return {
restrict: 'E',
template: '<child-directive></child-directive>',
controller: function($scope, $element){
this.variable = "Hi Vinothbabu"
}
}});app.directive('childDirective', function() {
return {
restrict: 'E',
template: '<h1>I am child</h1>',
replace: true,
require: '^parentDirective',
link: function($scope, $element, attr, parentDirectCtrl){
//you now have access to parentDirectCtrl.variable
}
}});- 3 回答
- 0 关注
- 600 浏览
添加回答
举报
