博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Angular Directive: link vs compile vs controller
阅读量:6413 次
发布时间:2019-06-23

本文共 2283 字,大约阅读时间需要 7 分钟。

hot3.png

Compile :

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.

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.

Linking phase :

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.

Controller :

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.

转载于:https://my.oschina.net/u/214483/blog/478976

你可能感兴趣的文章
给大家讲解一下JavaScript与后台Java天衣无缝相结合
查看>>
探索HTML5之本地文件系统API - File System API
查看>>
javascript有用代码块(1)
查看>>
libevent 笔记
查看>>
PHP实现人人OAuth登录和API调用
查看>>
redis源码笔记 - initServer
查看>>
FindBugs工具常见问题
查看>>
ECSHOP报错误Deprecated: preg_replace(): The /e modifier is depr
查看>>
【iOS】iOS之Button segue弹出popOver消除(dismiss)问题
查看>>
java多线程系列5-死锁与线程间通信
查看>>
数据库分库分表
查看>>
腾讯Hermes设计概要——数据分析用的是列存储,词典文件前缀压缩,倒排文件递增id、变长压缩、依然是跳表-本质是lucene啊...
查看>>
小程序模板嵌套以及相关遍历数据绑定
查看>>
Systemd入门教程:命令篇(转)
查看>>
java随机范围内的日期
查看>>
linux包之diff
查看>>
spring事务学习(转账案例)(二)
查看>>
[官方教程] [ES4封装教程]1.使用 VMware Player 创建适合封装的虚拟机
查看>>
http协议与http代理
查看>>
【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例...
查看>>