Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 - Make Last Item in Array A Link Using ngFor

I have a list of menu items and I want to make the last item in the array a link.

Right now the menu items are built from a component, but I'm unsure of how to make the last item in the array a link.

ActionMenuItem.component.html

  <div *ngIf="expanded">
  <actionmenuitem *ngFor="let child of line.children" [line]="child" (inWorkspace)="toWorkspace($event)"></actionmenuitem>

ActionMenuItem.Component.ts

  onSelect(){
// If it has children, expand them && flip carat.
if(this.line.children.length > 0){

  this.expanded = !this.expanded;

  if(this.iconName == "expand_more"){
    this.iconName = "expand_less"
  } else {
    this.iconName = "expand_more"
  }

} else {
  this.inWorkspace.emit(this.line);
}
like image 990
kjamp Avatar asked Sep 16 '25 07:09

kjamp


1 Answers

Angular exposes the following variables which you can make use of:

  • first
  • last
  • even
  • index
  • odd

So to make the the last item a link you can do this

<div *ngFor="let child of line.children; let last = islast">
   <actionmenuitem *ngIf="islast" [line]="child" 
(inWorkspace)="toWorkspace($event)">
    </actionmenuitem>
</div>
like image 62
Banki Louis Avatar answered Sep 17 '25 21:09

Banki Louis