Angularjs - 元素上的多个指令,其中一个是隔离范围 [英] Angularjs - Multiple directives on element with one being isolate scope

查看:24
本文介绍了Angularjs - 元素上的多个指令,其中一个是隔离范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人可以解释当前的行为.

Wondering if someone can explain the current behavior.

我有一个具有隔离作用域的指令,称为 dirA.然后我在它上面放了另一个指令,dirB.期望第二个指令获取隔离指令创建的每个范围.

I have one directive with isolate scope, call it dirA. I then put another directive on it, dirB. Expecting the second directive to get each scope that the isolate directive creates.

dirB 获取控制器范围.为什么它不会获得 dirA 为该元素创建的相同隔离范围?

dirB gets the controller scope. Why would it not get the same isolate scope that dirA is creating for that element?

感谢您的帮助.我真的没有任何代码,只是好奇.如果有人需要两个小指令和一些 html 来帮助可视化,我当然会.谢谢.

Thanks for any help. I don't really have any code for this, just curious. If anyone needs two little directives and some html to help visualize i will of course. thank you.

推荐答案

您观察到的情况是正确的,而且这种行为是设计使然.

What you observe is correct and this behavior is by design.

当指令请求一个隔离的作用域时,AngularJS 会专门为该指令创建一个作用域,不是为声明指令的元素创建一个作用域.换句话说,隔离是针对每个指令的,而不是针对每个元素的.这使得编写可重用和独立指令的代码比必须防止不当使用与其他指令隔离的范围要容易得多.

When a directive requests an isolated scope, AngularJS creates a scope for that directive specifically, not for the element where the directive is declared on. In other words, the isolation is per-directive, not per-element. This makes coding reusable and standalone directives much easier than if one has to guard against improper usage of the isolated scope from other directives.

在同一元素上声明的所有其他指令共享外部作用域.(同样值得注意的是,这些指令甚至不能请求自己的作用域,无论是否隔离.虽然由于错误,在 1.3 之前不能保证这种行为,但它已经 已在 1.3 中修复.)

All other directives declared on the same element share the outer scope. (It's also worth noting that these directives can't even request their own scope, isolated or not. While this behavior is not guaranteed prior to 1.3 due to a bug, it is already fixed in 1.3.)

这篇关于Angularjs - 元素上的多个指令,其中一个是隔离范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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