How to access previous / later items in ngFor loop?
<div *ngFor="let item of list"> <div *ngIf="item == previousItem"></div> <div *ngIf="item == firstItem"></div> </div> How can we make this type of work work? That is, how can we access other elements in the list either (1) with respect to the current index, or (2) with the absolute index?
EDIT: what if the list was instead of Observable?
<div *ngFor="let item of observable"> <div *ngIf="item == previousItem"></div> <div *ngIf="item == firstItem"></div> </div> +5
1 answer
You can use the index *ngFor
<div *ngFor="let item of list; let i = index;"> <div *ngIf="item == list[i-1]"></div> <div *ngIf="item == list[0]"></div> </div> You can find more information about *ngFor local variables in the docs: https://angular.io/docs/ts/latest/api/common/index/NgFor-directive.html
+7