Angular:在ngFor条件上插入新的.row

时间:2018-06-03 23:58:42

标签: html angular typescript bootstrap-4

我是Angular的新手,所以我决定尝试他们的教程,但有一些小改动。基本上我有以下数组:

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将它们列在屏幕上,所以我想方设法做到这一点,这对我来说是最合乎逻辑的事情,但它不起作用。

<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中的条件为真时,它不会添加行。我做错了什么?

1 个答案:

答案 0 :(得分:0)

声明heroes并将其更新为您希望以HTML格式显示,即连续3列

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。

<强> 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>