加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么ng-transclude的范围不是它的指令范围的子

发布时间:2020-12-17 08:36:30 所属栏目:安全 来源:网络整理
导读:给定一个指令(container1)与transclude和一个孤立的范围,当指令链接时,我有这些范围: Scope 004 -- scope of the body Scope 005 -- scope of directive container1 Scope 006 -- scope of the ng-transclude 我期望: Scope 004 -- scope of the body Sc
给定一个指令(container1)与transclude和一个孤立的范围,当指令链接时,我有这些范围:
Scope 004           <-- scope of the body
    Scope 005       <-- scope of directive container1
    Scope 006       <-- scope of the ng-transclude

我期望:

Scope 004            <-- scope of the body
    Scope 005        <-- scope of the directive
         Scope 006   <-- scope of the ng-transclude

如果同一个指令有一个共享作用域而不是一个孤立的作用域,我得到预期的结果。

这使我有一个问题,因为如果被转录的内容包含另一个指令(component1)与一个孤立的范围,我得到:

Scope 004             <-- scope of the body
    Scope 005         <-- scope of the directive
    Scope 006         <-- scope of the ng-transclude
          Scope 007   <-- scope of directive component1

我想使用像这样的指令:

<container1>
   <component1 data="objectExposedInContainer1"/>
</container1>

但是这不工作,在component1内部,$ scope.data是未定义的,因为objectExposedInContainer1不在正确的范围。

我有两个问题:

>为什么ng-transclude的范围不是它的指令范围的子元素,如果指令有一个孤立的范围?这是一个错误?
>如果它不是一个错误,容器指令如何传递数据到它的内容,如果不是通过设置属性,如我试过。

这里是一个不工作的示例:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview.因为Plunker是用Anguar构建的,所以很难用Batarang进行调试。我建议在本地下载代码。注释掉app.js的第10行,以使其使用共享作用域。

Why ng-transclude’s scope is not a child of its directive’s scope if the directive has an isolated scope?

ng-transclude旨在允许指令使用任意内容,隔离的范围旨在允许指令封装其数据。

如果ng-transclude没有保留这样的范围,那么任何被转录的任何内容都需要知道指令的实现细节(即它需要知道你创建的孤立范围上的可用内容)。

If it’s not a bug,how can a container directive pass data to it’s content,if not by setting attributes like I tried.

如果容器指令和包含的指令是耦合的 – 即你写了它们并且需要他们一起行动,那么它们应该通过共享控制器通信。

如果容器指令应该将内容注入到孩子的范围内(例如ng-repeat),那么你不应该使用一个孤立的范围。

角度文档很清楚的是什么行为应该是:

“In a typical setup the widget creates an isolate scope,but the transclusion is not a child,but a sibling of the isolate scope. This makes it possible for the widget to have private state,and the transclusion to be bound to the parent (pre-isolate) scope.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读