角度:在ngFor条件上插入新的.row [英] Angular: Insert new .row on ngFor condition
问题描述
我是Angular的新手,所以我决定尝试他们的教程,但是做了一些小的改动.基本上,我有以下数组:
I'm new to Angular so I decided to try their tutorial, but with some small alterations. Basically I have the following array:
export const HEROES: Hero[] = [
{ id: 11, name: 'Mr. Nice' },
{ id: 12, name: 'Narco' },
{ id: 13, name: 'Bombasto' },
{ id: 14, name: 'Celeritas' },
{ id: 15, name: 'Magneta' },
{ id: 16, name: 'RubberMan' },
{ id: 17, name: 'Dynama' },
{ id: 18, name: 'Dr IQ' },
{ id: 19, name: 'Magma' },
{ id: 20, name: 'Tornado' }
];
目标是使用* ngFor在屏幕上列出它们,因此我寻找了一些实现方法,这对我来说是最合乎逻辑的事情,但它不起作用.
And the objective is to list them on screen using *ngFor, so I looked up for some ways to do it and this was the most logical thing for me to do, but it's not working.
<div class="container-fluid bg-secondary padding-sm margin-sm rounded">
<div class="row">
<div *ngFor="let hero of heroes; let index = index">
<div class="col-lg-4 bg-lightblue rounded">
<p class="text-secondary lead">
<span class="text-info text-uppercase lead font-weight-bold">
{{ hero.name}}
</span>
Details
</p>
<p>
<span class="font-weight-bold text-success">
ID:
</span>
{{ hero.id }}
</p>
<p>
<span class="font-weight-bold text-success">
Name:
</span>
{{ hero.name }}
</p>
<label class="font-weight-bold">Change name:
<input [(ngModel)]="hero.name" class="input-md" type="text" placeholder="name">
</label>
</div>
{{ (index + 1) % 3 }}
<div class="row" *ngIf="(index + 1) % 3 == 0"></div>
</div>
</div>
</div>
我想做的是开始循环浏览列表,当列表达到3列时,它通过检查(index +1)%3 是否等于0并添加一行来继续添加下一行的列. 现在,当ngIf中的条件为true时,它不会添加行.我在做什么错了?
What I'm attempting to do is to start cycling through the list and when it reaches 3 columns, it adds a row by checking if (index + 1) % 3 equals 0 and continues adding columns on the next row. Right now it's not adding the row when the condition in ngIf is true. What am I doing wrong?
推荐答案
声明heroes
并根据要显示在HTML中的内容进行更新,即连续3列
declare heroes
and update it as you want to display in HTML i.e. 3 columns in a row
heroes = [];
HEROES.forEach((hero, index) => {
if(index % 3 == 0) {
let row = [];
row.push(hero);
this.heroes.push(row);
} else {
this.heroes[this.heroes.length - 1].push(hero);
}
});
它将生成heroes
为:
[
[ { "id": 11, "name": "Mr. Nice" }, { "id": 12, "name": "Narco" }, { "id": 13, "name": "Bombasto" } ],
[ { "id": 14, "name": "Celeritas" }, { "id": 15, "name": "Magneta" }, { "id": 16, "name": "RubberMan" } ],
[ { "id": 17, "name": "Dynama" }, { "id": 18, "name": "Dr IQ" }, { "id": 19, "name": "Magma" } ], [ { "id": 20, "name": "Tornado" } ]
]
然后,您可以根据需要更新HTML.
then you can update HTML as you want.
HTML
<div class="row" *ngFor="let rowData of heroes">
<div class="col-lg-4 bg-lightblue rounded" *ngFor="let hero of rowData">
<p class="text-secondary lead">
<span class="text-info text-uppercase lead font-weight-bold">{{ hero.name}}</span> Details
</p>
<p>
<span class="font-weight-bold text-success">ID: </span>{{ hero.id }}
</p>
<p>
<span class="font-weight-bold text-success">Name: </span>{{ hero.name }}
</p>
<label class="font-weight-bold">Change name:
<input [(ngModel)]="hero.name" class="input-md" type="text" placeholder="name">
</label>
</div>
</div>
这篇关于角度:在ngFor条件上插入新的.row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!