Remove divider after last item while using ngFor

2019-06-24 19:33发布

问题:

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?

回答1:

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.