简体   繁体   English

我什么时候应该在Angular中使用隔离范围?

[英]When should I use an isolate scope in Angular?

In the AngularJS guide it says: AngularJS指南中它说:

As the name suggests, the isolate scope of the directive isolates everything except models that you've explicitly added to the scope: {} hash object. 顾名思义,该指令的隔离范围隔离了除已明确添加到范围的模型之外的所有内容:{}哈希对象。 This is helpful when building reusable components because it prevents a component from changing your model state except for the models that you explicitly pass in. 这在构建可重用组件时很有用,因为它可以防止组件更改模型状态,但显式传入的模型除外。

...which sounds great. ......听起来很棒。 It seems like the best practice would be to try to use an isolate scope in all your directives to keep them encapsulated. 似乎最好的做法是尝试在所有指令中使用隔离范围来保持它们的封装。

However, I've found that if I try to add two directives with an isolate scope to the same element, Angular errors saying I can only have 1 directive with an isolate scope per element. 但是,我发现如果我尝试将两个带有隔离范围的指令添加到同一个元素,Angular错误说我只能有1个指令,每个元素有一个隔离范围。 That seems extremely limiting, and sort of defeats the purpose of an isolate scope. 这似乎是非常有限的,有点破坏了隔离范围的目的。

To me, you shouldn't have to worry about whether or not your directive will be used with other directives when deciding whether or not your scope is isolated. 对我而言,在决定您的范围是否被隔离时,您不必担心您的指令是否将与其他指令一起使用。

Am I missing something? 我错过了什么吗?

Should I be defaulting to non-isolate scopes? 我应该默认为非隔离范围吗? If so, is there a rule of thumb when I should be isolating my scope? 如果是这样,当我应该隔离我的范围时,是否有经验法则?

Generally, isolate scopes should be used on stand-alone directive - those directives that can't (and shouldn't) be modified by anything but themselves, no external influences, no add-ons (this includes modifying them by using another directive on their element, of course). 通常,隔离范围应该用于独立指令 - 那些不能(也不应该)被其他任何东西修改的指令,没有外部影响,没有附加组件(这包括通过使用另一个指令修改它们)他们的元素,当然)。

Why is this an issue? 为什么这是一个问题?

Say you have two directives with isolate scope on your element. 假设您的元素上有两个带隔离范围的指令。 Each of them has a property called cdmckay : 他们每个人都有一个名为cdmckay的属性:

<directive1 directive2 ng-show="cdmckay"></directive1>

Which one would have a higher priority, the one from directive1 or the one from directive2 ? 哪一个具有更高的优先级,一个来自directive1或一个来自directive2 Should the element be shown or not? 该元素是否应该显示?

There's no way to know whether the used value will be from the directive one or the directive two. 无法知道所使用的值是来自指令1还是指令2。 I guess it could be possible to combine multiple isolate scopes into a single one if they don't share property names (and only throw errors if they do), but I doubt Angular will ever take that path. 我想如果它们不共享属性名称可以将多个隔离范围组合成单个范围(如果它们共用则只抛出错误),但是我怀疑Angular会采用这种方式。

The way around this, in case your directives don't complement each other, is to use parent-child elements, so instead, say: 如果您的指令不相互补充,那么解决这个问题的方法是使用父子元素,所以请说:

<directive1 directive2></directive1>

you do this: 你做这个:

<directive1>
    <directive2></directive2>
</directive2>

I know it just doesn't seem right in many cases, but if you use isolate scope, this is one of the ways to combine directives. 我知道它在许多情况下似乎并不正确,但如果使用隔离范围,这是组合指令的方法之一。 But again, if you expect directive1 to be extended with something, then build it that way (one of the ways would be to avoid using the isolate scope on directive2 then). 但是,再次,如果你期望使用某些东西扩展directive1 ,那么以这种方式构建它(其中一种方法是避免在directive2上使用isolate scope)。

If you post your example, people will probably be able to give less generic answers. 如果你发布你的例子,人们可能会给出较少的通用答案。 I hope this will get you moving in the right direction. 我希望这会让你朝着正确的方向前进。


Brett's double-widget comment is spot-on: 布雷特的双重小部件评论是正确的:

If I have a calendar widget and a listview widget (that are implemented as directives with isolated scope), why on earth would I apply them to the same element? 如果我有一个日历小部件和一个listview小部件(实现为具有隔离范围的指令),为什么我会将它们应用于同一个元素?

我认为你可以把它们放在不同的元素上来解决这个问题。

<div my-custom-dir1 with-some-property="1"><span my-custom-dir2 with-some-property="2"></span></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM