为什么多个指令不能要求在同一个元素上有一个隔离的作用域? [英] Why can't multiple directives ask for an isolated scope on the same element?

查看:24
本文介绍了为什么多个指令不能要求在同一个元素上有一个隔离的作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 Angularjs - 一个元素上的多个指令隔离范围是对的,隔离范围绑定到指令,那么为什么会发生冲突?此错误的文档指出,处理它们会导致冲突或不受支持的配置.我不买这个.多个指令已经共享元素的范围,这肯定是冲突/不受支持的配置会出现的地方.我已经尝试寻找为什么",但空手而归.

If Angularjs - Multiple directives on element with one being isolate scope is right, the isolated scope is bound to the directive, so why would there be any clashes? The documentation for this error states that processing them would result in a collision or an unsupported configuration. I don't buy this. Multiple directives already share the element's scope, which is surely where clashes/unsupported configurations would come in. I've tried looking for the "why" on this, but have come up empty handed.

有人可以解释/举例说明这确实会造成碰撞或不支持的配置吗?

Can someone explain / give an example where this would indeed create a collision or an unsupported configuration?

推荐答案

为什么同一元素上的多个指令不能获得单独的隔离作用域

答案很简单——只需要一个作用域来将子元素绑定到 (见源代码),因为在后代元素中对作用域属性的赋值需要有一个明确的目标.剩下的就是措辞问题了.

Why can't multiple directives on the same element get separate isolated scopes

The answer is simple - there needs to be only one scope to bind the child elements to (see source), because assignments to scope properties done in descendant elements need to have a clear target. The rest is a question of wording.

虽然在某种程度上,引用正在为该特定指令"创建的隔离范围是合适的(正如链接答案所做的那样),只是在某种意义上,请求隔离的指令是唯一的该元素上的指令可以访问隔离范围.因此,创建范围是为了将指令和子元素与该级别"的其余部分隔离开来.DOM.

While it is appropriate, in a way, to refer to the isolate scope being created "for that particular directive" (as the linked answer does), it is only in the sense that the directive that requested the isolation is the only one of the directives on that element to have access to the isolated scope. So, the scope is created to isolate the directive and the child elements from the rest of that "level" of DOM.

为多个指令提供相同的隔离作用域可能会导致作用域绑定配置发生冲突(多个指令可能会尝试绑定到隔离作用域上的相同属性).

Giving multiple directives the same isolated scope would risk a clash of scope binding configurations (multiple directives could try to bind to the same property on the isolated scope).

一个简单而令人信服的论点是元素上的 {{interpolated.expressions}} 需要针对与 plain expressions(提供给指令支持他们),否则整个事情都会一团糟.({{expressions}} 的插值是单独完成的,因此可以配置一个指令接受一个属性中的 plain expression 和另一个属性中的 String使用针对不同范围评估的表达式.)

A simple and compelling argument is that the {{interpolated.expressions}} on an element need to be evaluated against the same scope as plain expressions (supplied to directives that support them), otherwise the whole thing would be a total mess. (Interpolation of {{expressions}} is done separately, so a directive accepting a plain expression in one attribute and String in another could be configured with expressions evaluated against different scopes.)

如果他们真的需要,他们可以访问隔离的范围(但这需要 调试数据 被启用).如果他们的优先级低于创建隔离范围的指令,他们可以在他们的链接函数中使用 element.isolateScope() (查看演示).

If they really need to, they can access the isolated scope (but this needs Debug Data to be enabled). If they have lower priority than the directive creating the isolate scope, they can just use element.isolateScope() in their linking function (see demo).

这篇关于为什么多个指令不能要求在同一个元素上有一个隔离的作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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