何时在Angular中使用transclude'true'和transclude'element'? [英] When to use transclude 'true' and transclude 'element' in Angular?

查看:99
本文介绍了何时在Angular中使用transclude'true'和transclude'element'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时应使用 transclude: true ,何时应使用 transclude: element
我在棱角文档中找不到有关 transclude:'element'的任何内容,它们非常令人困惑。



<如果有人能用简单的语言解释这一点,我会很高兴。
每个选项的好处是什么?它们之间的真正区别是什么?



这就是我发现的内容:


 后缀:true 

在编译函数中,您可以借助transclude链接功能来操作DOM,或者您可以使用任何HTML标签上的ngTransclude指令将已包含DOM的内容插入模板中。



  transclude:'element'

此元素将包含整个元素,而包含链接的功能为在编译功能中引入。您无法在此处访问范围,因为尚未创建范围。编译函数为该指令创建了一个链接函数,该链接函数可以访问范围,而transcludeFn可以让您触摸克隆的元素(已被嵌入)进行DOM操作或使用绑定到其中范围的数据。供您参考,它用于ng-repeat和ng-switch。



解决方案

AngularJS指令文档


transclude -编译元素的内容并将其提供给指令。通常与ngTransclude一起使用。包含的优势在于,链接功能可以接收预先绑定到正确范围的包含功能。在典型的设置中,小部件创建隔离范围,但是包含不是子对象,而是隔离范围的同级对象。这样便可以使小部件具有私有状态,并且可以将包含项绑定到父级(隔离前)范围。


true -包含指令的内容。



'element'-包含整个元素,包括以较低优先级定义的所有指令。





transclude:true



因此,假设您有一个名为 my-transclude-true 的指令,该指令声明了 transclude :true 看起来像这样:

 < div> 
< my-transclude-true>
< span> {{something}}< / span>
{{otherThing}}
< / my-transclude-true>
< / div>

在编译之后和链接之前,它变为:

 < div> 
< my-transclude-true>
< ;!-已包含->
< / my-transclude-true>
< / div>

我的内容(孩子) transclude-true < span> {{something}}< / span> {{... 已被伪装并可用于该指令。



伪装:'element'



如果您有一个名为 my-transclude-element 的指令,并声明了 transclude:'element'看起来像这样:

 < div> 
< my-transclude-element>
< span> {{something}}< / span>
{{otherThing}}
< / my-transclude-element>
< / div>

在编译之后和链接之前,它变为:

 < div> 
< ;!-已包含->
< / div>

此处,整个元素(包括其子元素)已被排除并提供给



链接后会发生什么?



这取决于您的指令执行所需的操作具有transclude功能。 ngRepeat 使用 transclude:'element',以便在范围更改时可以重复整个元素及其子元素。但是,如果只需要替换标记并希望保留其内容,则可以将 transclude:true ngTransclude 指令为您执行此操作。


When should I use transclude: 'true' and when transclude: 'element' ? I cant find anything about transclude: 'element' in the angular docs, they are pretty confusing.

I would be happy if someone could explain this in simple language. What is the benefit of each option? What is the real difference between them?

This is what I have found :

transclude: true

Inside a compile function, you can manipulate the DOM with the help of transclude linking function or you can insert the transcluded DOM into the template using ngTransclude directive on any HTML tag.

and

transclude: ‘element’

This transcludes the entire element and a transclude linking function is introduced in the compile function. You can not have access to scope here because the scope is not yet created. Compile function creates a link function for the directive which has access to scope and transcludeFn lets you touch the cloned element (which was transcluded) for DOM manipulation or make use of data bound to scope in it. For your information, this is used in ng-repeat and ng-switch.

解决方案

From AngularJS Documentation on Directives:

transclude - compile the content of the element and make it available to the directive. Typically used with ngTransclude. The advantage of transclusion is that the linking function receives a transclusion function which is pre-bound to the correct scope. 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.

true - transclude the content of the directive.

'element' - transclude the whole element including any directives defined at lower priority.

transclude: true

So let's say you have a directive called my-transclude-true declared with transclude: true that looks like this:

<div>
  <my-transclude-true>
    <span>{{ something }}</span>
    {{ otherThing }}
  </my-transclude-true>
</div>

After compiling and before linking this becomes:

<div>
  <my-transclude-true>
    <!-- transcluded -->
  </my-transclude-true>
</div>

The content (children) of my-transclude-true which is <span>{{ something }}</span> {{..., is transcluded and available to the directive.

transclude: 'element'

If you have a directive called my-transclude-element declared with transclude: 'element' that looks like this:

<div>
  <my-transclude-element>
    <span>{{ something }}</span>
    {{ otherThing }}
  </my-transclude-element>
</div>

After compiling and before linking this becomes:

<div>
   <!-- transcluded -->
</div>

Here, the whole element including its children are transcluded and made available to the directive.

What happens after linking?

That's up to your directive to do what it needs to do with the transclude function. ngRepeat uses transclude: 'element' so that it can repeat the whole element and its children when the scope changes. However, if you only need to replace the tag and want to retain it's contents, you can use transclude: true with the ngTransclude directive which does this for you.

这篇关于何时在Angular中使用transclude'true'和transclude'element'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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