可以在ngFor内部使用ng-content吗? [英] Can an ng-content be used inside of an ngFor?
问题描述
我们想在*ngFor
内放置一个ng-content
,然后在使用包含ngFor的组件时指定ng-content
的内容.有可能吗?
We'd like to put a ng-content
inside of an *ngFor
and then specify the contents of the ng-content
when the component containing the ngFor is used. Is it possible?
<li *ngFor="let data of dataSource">
<ng-content></ng-content>
</li>
推荐答案
可能,但可能不会产生期望的结果.由父级传递的子代只能投影一次(无论那里有多少<ng-content>
.如果<ng-content>
元素未使用select
属性选择传递的子代的特定部分和不同部分,则所有项目都将投影到带有默认选择器的第一个<ng-content>
(无).
It is possible but probably doesn't produce the desired result. Children passed by the parent can only projected once (no matter how many <ng-content>
are there. If the <ng-content>
elements don't select specific and different parts of the passed children using the select
attribute, then everything is projected to the first <ng-content>
with the default selector (none).
要执行此操作,您可能需要一个自定义的ngFor
,该自定义ngFor
重复传递给<ng-content>
的内容.
To make this work you probably want a custom ngFor
that repeats the content passed to <ng-content>
.
NgFor
s ngForTemplate
可能对您的用例有所帮助.
NgFor
s ngForTemplate
might help in your use case.
另请参见 Angular2子组件作为数据
这篇关于可以在ngFor内部使用ng-content吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!