- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要对范围和模板执行一些操作。看来我可以在 link
函数或 controller
函数中做到这一点(因为两者都可以访问范围)。
什么时候我必须使用 link
函数而不是 Controller ?
angular.module('myApp').directive('abc', function($timeout) {
return {
restrict: 'EA',
replace: true,
transclude: true,
scope: true,
link: function(scope, elem, attr) { /* link function */ },
controller: function($scope, $element) { /* controller function */ }
};
}
另外,我知道 link
是非 Angular 世界。所以,我可以使用 $watch
、$digest
和 $apply
。
link
函数的意义是什么,当我们已经有了 Controller 的时候?
最佳答案
在我与 link
的初始斗争之后和 controller
函数并阅读了很多关于它们的内容,我想现在我有了答案。
首先让我们了解,
简而言之, Angular Directive(指令)是如何工作的:
我们从一个模板开始(作为一个字符串或加载到一个字符串)
var templateString = '<div my-directive>{{5 + 10}}</div>';
现在,这个 templateString
被包装为 Angular 元素
var el = angular.element(templateString);
与 el
, 现在我们用 $compile
编译它取回 link 功能。
var l = $compile(el)
这就是发生的事情,
$compile
遍历整个模板并收集它识别的所有指令。link
收集函数。link
函数被包装在一个新的 link
中函数并返回为 l
.最后,我们提供scope
函数到此l
(link) 函数,它使用 scope
进一步执行包装的链接函数及其对应的元素。
l(scope)
这增加了 template
作为 DOM
的新节点并调用 controller
它将其 watch 添加到与 DOM 中的模板共享的 范围。
比较 compile vs link vs controller:
每个指令只编译一次,link 函数被保留以供重复使用。因此,如果有适用于指令的所有实例的东西,应该在指令的 compile
内执行。功能。
现在,编译后我们有 link
在将 template 附加到 DOM 时执行的函数。因此,我们执行特定于指令每个实例的所有操作。例如:附加事件、根据范围改变模板等
最后,controller 可以在指令作用于 DOM
时提供实时和 react 性。 (附上后)。因此:
(1) 使用链接设置 View [V](即模板)后。 $scope
是我们的 [M] 和 $controller
是我们在 M V C
(2) 通过设置监视来利用 2-way 与 $scope 的绑定(bind)。
(3) $scope
预计将在 Controller 中添加监视,因为这是在运行时监视模板的内容。
(4) 最后,controller
也用于能够在相关指令之间进行通信。 (如 myTabs
中的示例 https://docs.angularjs.org/guide/directive )
(5) 确实,我们可以在 link
中完成所有这些操作。功能也一样,但它是关于关注点分离。
因此,最后我们得到了完美契合所有部分的以下内容:
关于javascript - Angular JS : What is the need of the directive’s link function when we already had directive’s controller with scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20018507/
我通常从以下位置获取代码:https://www.w3schools.com .这个代码还有一个 Accordion ; 但是,当我使用链接 -> 到 Accordion 时, Accordion 不
我见过的所有 JNI 指南(例如 JNI runtime linking )都建议我将 JNI 库链接到 libjvm.so: $ gcc -I${JAVA_HOME}/include -c -o t
实现以下目标的正确 URL 格式是什么: 使用 Universal Link 在 iOS 上的另一个应用程序中打开 Google map 应用程序。 根据两个坐标设置目的地:纬度和经度,并让用户选择交
我已经查看了 Microsoft 的 MSDN 和整个网络,但我仍然无法很好地了解它是什么。 这是否意味着已完成的程序在执行期间的不同时间加载 DLL,而不是在启动时一次性加载所有 DLL? 我完全偏
我有一个看起来像这样的 css: .browse-link A:link { color: #F6CA4C; text-decoration: none; } .browse-link
当我点击“产品”链接时,它突然指向#link。我怎样才能使它的外观看起来像滚动然后转到产品?请帮帮我。 vStudy function big(x){
我想在保存之前更改从输入字段中获取的值。 params[:link]['url'] = "www.facebook.com/redbull" 现在我只想将“redbull”放入数据库。以下代码失败,因
我正在使用链表编写程序(真是一场噩梦)。 无论如何,该程序的目的是输入 8 个字符,然后让程序将字符打印回给您,并以相反的顺序打印回字符,当然是使用链表。 到目前为止我已经明白了。它有很多错误(我认为
基本上,我删除了 anchor 按钮,因此链接窗口中不应该有指向 anchor 选项的链接。 有什么方法可以删除该下拉选项 ? 最佳答案 想通了 if ( dialogName == 'link' )
我的本地(和远程)SQL SERVER 2005 管理员都声称“允许链接服务器是一个安全问题”并禁止在此处使用它们。 (哈?) 无论如何,有没有办法在没有链接服务器的情况下做类似的事情? SELE
如果我有: linkedlist1= 1,2,3,4; 和 linkedlist2= 5,6,7; 如果我调用: linkedlist2.set(0,9999) 它会更改为 linkedlist2 =
首先,如果这个问题看起来很愚蠢,我很抱歉,但我仍在学习 React 和 html。所以问题是我的 react 代码中有一个按钮标签,它为我提供了一些关于进入我提供的特定链接的逻辑。我的 Button.
我将制作一个文本 block ,这样如果您单击一个单词,它就会被词汇替代品所替换。 例如“fearful-of-cats”是“ailurophobic”的词汇替代品,因为如果您在任何文本中将后者替换为
我有以下代码 Can you click me? 目标是我可以点击“你能点击我吗”框并转到 google 并在包含 div 的任何其他地方
这是一个案例: 默认情况下,如果我点击#2、#3、#4、#5,我将被重定向到#1。 如果我想在没有 #1 激活的情况下点击输入,我该如何修复 CSS? 提前
有没有什么快速的方法可以使 :visited 链接的颜色与链接本身的颜色相同? 例如: * {color:black} a:link {color:blue} a:visited {color:inh
我读到从 iOS 9 开始,引入了通用链接。请解释深层链接和通用链接之间的区别。我的目标是,一个链接将通过邮件发送给客户。让邮件说有一个项目 A 的报价和一个链接。单击链接时 如果安装了该应用程序,则
因此我们需要对 CSS anchor 伪类使用以下顺序 a:link { color: red } a:visited { color: blue } a:hover { color
我组件的当前路径是http://localhost:3000/dashboard/questionnaire/5bf79ff4c45a150015cef7a9在这个组件里面有 Financials 如
我 rsync 目录“Promotion”包含两台具有不同目录结构的机器之间的绝对符号链接(symbolic link)。因此绝对符号链接(symbolic link)在两台机器上都不起作用。为了使它
我是一名优秀的程序员,十分优秀!