在Angular 2中,我得到了一个项目列表,并根据给定的条件(即基于位置编号),我将该列表设置为重复。Angular 2使用* ngFor和ngIf隐藏第一个元素
我需要隐藏“删除文本”为每个位置的第一个框。
Plunker: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview:
[1]: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview
import {Component} from '@angular/core';
@Component({
\t selector: 'app',
\t template: `
\t
<ul *ngFor="let locdata of locations">
<template ngFor let-item [ngForOf]="items"; let-i=index>
<div *ngIf="item.location==locdata.location">
<div class="title"> location {{ item.location}} <span *ngIf="isNotFirst(item)"> remove </span> </div>
<div class="container"> {{ item.sedule}}</div>
</div>
</template>
\t </ul>
\t
\t `
})
export class App {
items: string[];
isNotFirst(item: any) {
return this.items.filter(i => i.location === item.location).map(i => i.id).indexOf(item.id) !== 0;
}
locations:any;
constructor() {
this.locations=[{location:1},{location:2},{location:3}];
this.items = [{
id:1,
location:1,
sedule:1
},
{
id:2,
location:2,
sedule:1
},
{
id:3,
location:1,
sedule:2
},
{
id:4,
location:2,
sedule:2
},
{
id:5,
location:1,
sedule:2
},
{
id:6,
location:2,
sedule:3
}, {
id:7,
location:3,
sedule:1
},
{
id:8,
location:3,
sedule:2
},
{
id:9,
location:3,
sedule:3
}];
}
}
我想你可以添加一个类到'first'元素并通过CSS隐藏它? (见:http://blog.angular-university.io/angular-2-ngfor/) – ochi
你将如何添加我无法得到的逻辑。 –
我们可以使用[hidden]也可以,但[hidden] =不能找到的条件。 –