可以在 ngFor 中使用 ng-content 吗? [英] Can an ng-content be used inside of an ngFor?
问题描述
我们想在 *ngFor
里面放一个 ng-content
然后指定 ng-content
的内容,当使用包含 ngFor 的组件.可能吗?
<ng-content></ng-content>
这是可能的,但可能不会产生预期的结果.父级传递的子级只能投影一次(无论有多少
.如果
元素没有选择使用 select
属性传递子项的特定和不同部分,然后使用默认选择器(无)将所有内容投影到第一个
.>
为了完成这项工作,您可能需要一个自定义 ngFor
来重复传递给
的内容.
NgFor
s ngForTemplate
可能帮助您的用例.
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>
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).
To make this work you probably want a custom ngFor
that repeats the content passed to <ng-content>
.
NgFor
s ngForTemplate
might help in your use case.
See also Angular2 child component as data
这篇关于可以在 ngFor 中使用 ng-content 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!