Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert divider in the `ngFor` in Angular?

I have a subsection and I'm wondering how I can place html divider between every subsection but not sure how to do that so I would be really appreciated if I can get any suggestion or help.

<div>
    <a mat-list-item *ngFor="let subsection of section.subSections" (click)="navigateToSubsection(section.id,subsection.id)">{{subsection.sectionName}}
    </a>
</div>

For example something like this:

enter image description here


2 Answers

You could use <hr> tag to render horizontal lines and *ngFor directive's last local variable to avoid rendering the line after the last item.

Try the following

<div>
  <a *ngFor="let subsection of section.subSections; let last=last"
    (click)="navigateToSubsection(section.id,subsection.id)">
    {{ subsection.sectionName }}
    <hr *ngIf="!last" class="solid">
  </a>
</div>

Working example: Stackblitz

like image 61
ruth Avatar answered Oct 29 '25 06:10

ruth


I created a sample for you on Stackblitz

You can use <hr> like this:

<div clas="main-container" *ngFor="let subsection of section.subSections>
   <a mat-list-item"
    (click)="navigateToSubsection(section.id,subsection.id)">{{subsection.sectionName}}
   </a>
   <hr/>
</div>
like image 28
ng-hobby Avatar answered Oct 29 '25 05:10

ng-hobby



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!