Angular-显示 *ngfor在行和列中

发布于 2025-01-19 02:07:04 字数 990 浏览 5 评论 0原文

我正在学习使用 Angular,但我对如何在列中显示数据有疑问。

在我的 app.components.ts 中,我有:

<div class="cardComp" *ngFor="let person of people">
  <app-card-person [person]="person"></app-card-person>
</div>

// people is an array of objects 

在我的 card-person.component.ts 中

<div class="card" style="width: 18rem;">
    <!-- <img src="..." class="card-img-top" alt="..."> -->
    <div class="card-body">
        <h5 class="card-title">{{person?.name}} {{person?.surname}}</h5>
        <p class="card-text">{{person?.description}}</p>
        <!-- <a href="#" class="btn btn-primary">Go somewhere</a> -->
    </div>
</div>

,通过这种方式,我获得了卡片列表,但每行 1 张:

CARD1
CARD2
CARD3
.....

我想要获得的是:

CARD1                        CARD2
CARD3                        CARD4
.....                        .....

I'm learning to use Angular and I have a problem with how to show data in columns.

In my app.components.ts I have:

<div class="cardComp" *ngFor="let person of people">
  <app-card-person [person]="person"></app-card-person>
</div>

// people is an array of objects 

In my card-person.component.ts

<div class="card" style="width: 18rem;">
    <!-- <img src="..." class="card-img-top" alt="..."> -->
    <div class="card-body">
        <h5 class="card-title">{{person?.name}} {{person?.surname}}</h5>
        <p class="card-text">{{person?.description}}</p>
        <!-- <a href="#" class="btn btn-primary">Go somewhere</a> -->
    </div>
</div>

In this way, I obtain a list of cards but they are 1 per row:

CARD1
CARD2
CARD3
.....

What I would to obtain is:

CARD1                        CARD2
CARD3                        CARD4
.....                        .....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回忆那么伤 2025-01-26 02:07:04

解决方案 1:使用 Flexbox

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 50%;
}
<div class="container">
  <div class="cardComp item" *ngFor="let person of people">
    <app-card-person [person]="person"></app-card-person>
  </div>
</div>

结果

示例解决方案StackBlitz 排名第一

建议阅读这篇 Flexbox 文章。


解决方案 2:使用 Bootstrap

先决条件:导入 Bootstrap CSS 文件。

<div class="row">
  <div class="cardComp col-6" *ngFor="let person of people">
    <app-card-person [person]="person"></app-card-person>
  </div>
</div>

结果

示例解决方案 2堆栈闪电战

Solution 1: Work with flexbox

.container {
  display: flex;
  flex-wrap: wrap;
}

.item {
  flex: 50%;
}
<div class="container">
  <div class="cardComp item" *ngFor="let person of people">
    <app-card-person [person]="person"></app-card-person>
  </div>
</div>

Result

Sample Solution 1 on StackBlitz

Recommended to read this Flexbox article.


Solution 2: Work with Bootstrap

Pre-requisites: Import Bootstrap CSS file.

<div class="row">
  <div class="cardComp col-6" *ngFor="let person of people">
    <app-card-person [person]="person"></app-card-person>
  </div>
</div>

Result

Sample Solution 2 on StackBlitz

悲歌长辞 2025-01-26 02:07:04

CSS 网格

在你的 app.component.scss 中

.cardComp {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px; // gap between columns
}

CSS Grid

In your app.component.scss

.cardComp {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px; // gap between columns
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文