Angular-在 *ngfor中只有一个ng -content的项目不同的内容
我希望NGFOR在迭代中使用一个儿童组件:
卡组件,< card>
:
<div *ngFor="let item of items">
<ng-content [value]="item">
</ng-content>
</div>
使用卡组件:
<card>
<child-comp1> Hello </child-comp1>
<child-comp2> Goodbye </child-comp2>
<child-comp3> See you </child-comp3>
...
</card>
预期结果:
<card>
<child-comp1> Hello item1</child-comp1>
<child-comp2> Goodbye item2</child-comp2>
<child-comp3> See you item3</child-comp3>
...
</card>
当然应该有和数据一样多的数据NGFOR数组使其正常工作。
编辑:上面的景象不代表实际使用的数据和组件,仅在这里说明和解释这个问题。 可能有或少于3个儿童组件,它们比显示值更复杂。
I'd want the ngfor to use one child component per iteration :
Card component, <card>
:
<div *ngFor="let item of items">
<ng-content [value]="item">
</ng-content>
</div>
Use of the card component :
<card>
<child-comp1> Hello </child-comp1>
<child-comp2> Goodbye </child-comp2>
<child-comp3> See you </child-comp3>
...
</card>
Expected result :
<card>
<child-comp1> Hello item1</child-comp1>
<child-comp2> Goodbye item2</child-comp2>
<child-comp3> See you item3</child-comp3>
...
</card>
Of course there should be as much child components as data in the ngFor array for it to work correctly.
EDIT : the above exemple doesn't represent the actual datas and components used, it is only here to illustrate and explain the question.
There could be more or less than 3 child components and they are more complex than just displaying values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尽力帮助您。
首先,我创建了一个主类,该类将调用所有卡组件。
main.component.html
main.component.ts
例如,您只需要在此处使用数据:
在您的卡组件中:
card.component.html
card.component.ts
(需要输入)
I will try to help you.
First of all I created a main class that will call all the card components.
main.component.html
main.component.ts
you only need to have your data here, for instance:
and in your card component:
card.component.html
card.component.ts
(Import Input is required)