我在 Angular 项目中有一些代码,它们使用两个具有隔离范围的单独指令。它们不需要共享范围,只需存在于同一元素上即可。它们都以稍微不同的方式改变 DOM,并且重要的是绑定到作为参数传递的值。
这在 1.0 中有效,但是 Angular 1.2 现在在尝试执行此操作时会生成错误
多个指令要求新的/隔离的范围
根据项目 git 历史记录,Angular 1.2 更改了行为,以将同一元素上的两个独立指令分开。这是一件好事,并且在将两个“Attribute”指令放在同一元素上时它可以正常工作。
i.e.
<div my:directive="myDirectiveData" my:other-directive="myOtherDirectiveData" />
正如您所期望的那样工作。
however
<my:directive my:directive-data="myDirectiveData" my:other-directive="myOtherDirectiveData" />
抛出上述错误。 (多个指令要求新的/独立的范围)
在这种情况下,我预计每个指令仍然与其自己的非共享隔离范围并行存在。
这在 Angular 1.2 中仍然可能吗?
在同一元素上定义多个指令时会发生什么情况的摘要:
Scenario directive #1 directive #2 Result
1 no new scope no new scope Both directives use the controller's scope.
(This should be obvious.)
2 new scope new scope Both directives share one new child scope.
3 new scope no new scope Both directives share one new child scope.
Why does dir #2 use the child scope?
This seems odd to me.
4 isolate scope no new scope Angular v1.0: both directives share the
isolate scope.
Angular v1.2+: dir #1 uses the isolate scope,
dir #2 uses the controller's scope.
请注意,不允许出现以下情况(Angular 会抛出错误):
Scenario directive #1 directive #2
5 isolate scope new scope
6 isolate scope isolate scope
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)