Angular-在 *ngfor中只有一个ng -content的项目不同的内容

发布于 2025-01-26 12:01:34 字数 894 浏览 4 评论 0原文

我希望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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2025-02-02 12:01:34

我会尽力帮助您。

首先,我创建了一个主类,该类将调用所有卡组件。
main.component.html

<div *ngFor="let item of items">
  <app-card [values]="item">
  </app-card>
</div>

main.component.ts
例如,您只需要在此处使用数据:

items: string[][] = [['it1.1','it1.2','it1.3'],['it2.1','it2.2','it2.3'],['it3.1','it3.2','it3.3']];

在您的卡组件中:
card.component.html

<ul>
    <li>Hello {{values[0]}}</li>
    <li>Goodbye {{values[1]}}</li>
    <li>See you {{values[2]}}</li>
</ul>

card.component.ts
(需要输入)

import { Component, OnInit, Input } from '@angular/core';
@Input() values: string[] = [];

I will try to help you.

First of all I created a main class that will call all the card components.
main.component.html

<div *ngFor="let item of items">
  <app-card [values]="item">
  </app-card>
</div>

main.component.ts
you only need to have your data here, for instance:

items: string[][] = [['it1.1','it1.2','it1.3'],['it2.1','it2.2','it2.3'],['it3.1','it3.2','it3.3']];

and in your card component:
card.component.html

<ul>
    <li>Hello {{values[0]}}</li>
    <li>Goodbye {{values[1]}}</li>
    <li>See you {{values[2]}}</li>
</ul>

card.component.ts
(Import Input is required)

import { Component, OnInit, Input } from '@angular/core';
@Input() values: string[] = [];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文