Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove divider after last item while using ngFor

I'm using this code to show a number of locations:

<div class="location" *ngFor="let item of location_array">
   {{item}} <hr>
</div>

which results in all the locations, separated by an horizontal rule. How can I edit this to get the exact same result, but without the last horizontal rule?

like image 989
binoculars Avatar asked Mar 16 '18 15:03

binoculars


2 Answers

Use last provided by the *ngFor along with *ngIf on your <hr>:

<div class="location" *ngFor="let item of location_array; let lastItem = last;">
   {{item}} <hr *ngIf="!lastItem">
</div>

Here is a StackBlitz Demo.

like image 53
Faisal Avatar answered Oct 25 '22 20:10

Faisal


In the current angular version (11) the syntax changed to the following:

<div *ngFor="let item of location_array; last as isLast">
    {{ item }} <hr *ngIf="!isLast">
</div>

See https://angular.io/api/common/NgForOf#local-variables

like image 25
bene-we Avatar answered Oct 25 '22 20:10

bene-we