【发布时间】:2017-05-25 00:16:36
【问题描述】:
在 Angular 2 中,我得到了一个项目列表,并根据给定条件(即基于位置编号)将列表设置为重复。
我需要为每个位置的第一个框隐藏“删除文本”。
Plunker: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview:
[1]: https://plnkr.co/edit/xtX5BjTBjO61sD2tLwWN?p=preview
import {Component} from '@angular/core';
@Component({
selector: 'app',
template: `
<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>
</ul>
`
})
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隐藏它? (见:blog.angular-university.io/angular-2-ngfor) -
你将如何添加我无法获得的逻辑。
-
我们也可以使用 [hidden] 但 [hidden]= 条件无法找到。
标签: javascript angular angular2-template angular2-directives