如何在NgFor中比较当前行和下一行

时间:2019-05-05 04:05:15

标签: angular ngfor

我正在尝试将当前行与下一行进行比较

这是我的代码:

<ng-container *ngFor="let c of my_tab; let i=index">
  <ng-container *ngIf="c.id != my_tab[i+1].id">
    //do_something
  </ng-container>
</ng-container>

但是它不起作用!

2 个答案:

答案 0 :(得分:0)

尝试重现您的代码,它可以正常工作。

my_tab = [{id:1, name:'C'},{id:2, name:'A'}, {id:2, name:'A'}, {id:3, name:'A'}];


<div *ngFor="let c of my_tab; let i=index">
  <div *ngIf="c.id != my_tab[i+1].id">
    not same
  </div>
</div>

https://stackblitz.com/edit/angular-index-next-last

答案 1 :(得分:0)

我已经解决了问题:

<ng-container *ngFor="let c of my_tab; let i=index">
  <ng-container *ngIf="c.id != my_tab[i+1]?.id">
    //do_something
  </ng-container>
</ng-container>