简体   繁体   English

Angular4-跳过* ngFor上的重复值

[英]Angular4 - skip repeating values on *ngFor

Trying to update a Angularjs project to Angular4. 尝试将Angularjs项目更新为Angular4。 I have a JSON rest endpoint that provides a list of activities by date and time. 我有一个JSON rest终结点,它按日期和时间提供活动列表。 in AngularJS, I used ng-repeat to iterate the data and ng-show to skip repeating data using [$index-1]. 在AngularJS中,我使用ng-repeat迭代数据,而ng-show使用[$ index-1]跳过了重复数据。 what's the proper way to accomplish the same result in angular4? 在angular4中完成相同结果的正确方法是什么?

Old code
<div ng-repeat="x in catview">
    <div ng-show="x.date != catview[$index -1].date">
        <h3>{{x.date | date: 'EEEE, MMM d'}}</h3>
    </div>
    <div class="{{x.category}}">
        <i class="{{x.category}}" ></i>
    </div>
    <div>
        <p>{{x.description}}</p>
        <p>{{x.time | date: 'shortTime' }} at {{x.place}}</p>
    </div>
</div>

the data looks like:
[{date:2017-06-23,category:dining,description:dinner, 
time:15:00,place:central},
{date:2017-06-23,category:shopping,description:walmart, 
time:15:30,place:central},
{date:2017-06-24,category:sleeping,description:hotel, 
time:15:00,place:central},
{date:2017-06-24,category:travel,description:bus ride, 
time:16:00,place:depot},]

and should produce: 并应产生:

June, 23 2017
  15:00 dinner at central
  15:30 shopping at walmart
June, 24 2017
  15:00 sleeping at hotel
  16:00 bus ride at depot

any help? 有什么帮助吗?

should be somehow like this: 应该是这样的:

<div *ngFor="let x of catview; index as i; first as isFirst">
  <div *ngIf="isFirst || x.date != catview[i -1].date">
    <h3>{{x.date | date: 'EEEE, MMM d'}}</h3>
  </div>
  <div [ngClass]="x.category"><i [ngClass]="x.category"></i></div>
  <div>
    <p>{{x.description}}</p>
    <p>{{x.time | date: 'shortTime' }} at {{x.place}}</p>
  </div>
</div>

pay attention they recommend to use already ngForOf instead of ngFor : https://angular.io/api/common/NgFor , just give it a try! 请注意,他们建议您使用已有的ngForOf代替ngForhttps : ngFor ,只需尝试一下!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM