问题描述
当我应该使用 transclude:真正的
当 transclude:元素
?
我不能找到什么transclude:元素
在角文档,它们是pretty混乱
我会很高兴,如果有人可以用简单的语言解释这一点。
什么是每个选项的好处?它们之间的真正区别是什么?
这是我发现:
and
From AngularJS Documentation on Directives:
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.
这篇关于AngularJS:何时使用transclude“真”和transclude“元素”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!