AngularJS:链接VS编译VS控制器 [英] AngularJS : link vs compile vs controller

查看:135
本文介绍了AngularJS:链接VS编译VS控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你创建一个指令,就可以把code到编译器,链接功能或控制器。
在文档他们解释说:

when you create a directive, you can put code into the compiler, the link function or the controller. In the docs they explain that:


  • 编译和链接功能角度的不同阶段使用
    周期

  • 控制器指令之间共享。

不过,对我来说是不明确的,哪种code应该去的地方。例如。:
我可以创建编译功能,让他们连接到链路的范围。或者我只重视功能控制器范围有多大?如何控制器指令之间共享,如果每个指令都可以有自己的控制器?真的是控制器共享或只是范围属性?

However, for me it is not clear, which kind of code should go where. E.g.: I can create functions in compile and have them attached to the scope in link. Or I only attach functions to the scope in the controller? How are controllers shared between directives, if each directive can have its own controller? Are really the controllers shared or is it just the scope properties?

推荐答案

编译:

这就是角实际上编译你的指令的阶段。此编译功能对给定的指令,每一个引用称为一次。例如,假设你使用的是NG重复指令。纳克重复将不得不查找它连接到,提取它连接到的HTML片段和创建模板函数的元素。

This is the phase where Angular actually compiles your directive. This compile function is called just once for each references to the given directive. For example, say you are using the ng-repeat directive. ng-repeat will have to look up the element it is attached to, extract the html fragment that it is attached to and create a template function.

如果您已经使用车把,强调模板或等同,它像编译他们的模板中提取出一个模板函数。这个模板函数传递数据和函数的返回值是在正确的地方的数据的HTML。

If you have used HandleBars, underscore templates or equivalent, its like compiling their templates to extract out a template function. To this template function you pass data and the return value of that function is the html with the data in the right places.

编译阶段是在角这一步返回模板函数。此在角度模板函数被调用链接函数。

The compilation phase is that step in Angular which returns the template function. This template function in angular is called the linking function.

链接阶段:

链接阶段是你的数据($范围内)连接到连接功能,它应该返回你的链接的HTML。由于该指令还指定了这个网站去还是什么它的变化,这已经是好去。这是你想要更改链接的HTML,即已经具有附加给它的数据的HTML功能。在角如果你写code在链接功能,其一般的链接后功能(默认情况下)。这是怎样的一个链接功能与模板链接后的数据被调用回调。

The linking phase is where you attach the data ( $scope ) to the linking function and it should return you the linked html. Since the directive also specifies where this html goes or what it changes, it is already good to go. This is the function where you want to make changes to the linked html, i.e the html that already has the data attached to it. In angular if you write code in the linking function its generally the post-link function (by default). It is kind of a callback that gets called after the linking function has linked the data with the template.

控制器:

该控制器是你把一些指令特定逻辑的地方。这个逻辑可以进入链接功能以及,但你将不得不把这一逻辑的适用范围,使其共享。但问题是,你然后将破坏你的指示的东西的范围是不是真的东西是预期。
那么,什么是选择,如果两个指令要相互交谈/互相合作呢? Ofcourse,你可以把所有的逻辑放到一个服务,然后使这两个指令依赖于该服务,但只是带来一个更多依赖。另一种方法是提供此范围控制器(通常是孤立的范围?),然后该控制器被注入到另一个指令时指令要求另外一个。见angularjs.org的第一页卡和窗格的例子。

The controller is a place where you put in some directive specific logic. This logic can go into the linking function as well, but then you would have to put that logic on the scope to make it "shareable". The problem with that is that you would then be corrupting the scope with your directives stuff which is not really something that is expected. So what is the alternative if two Directives want to talk to each other / co-operate with each other? Ofcourse you could put all that logic into a service and then make both these directives depend on that service but that just brings in one more dependency. The alternative is to provide a Controller for this scope ( usually isolate scope ? ) and then this controller is injected into another directive when that directive "requires" the other one. See tabs and panes on the first page of angularjs.org for an example.

这篇关于AngularJS:链接VS编译VS控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆