4

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);
}
2
  • 1
    What exactly do you mean when you say make it a link? Commented Oct 28, 2019 at 5:36
  • I’m trying to use the router to link to another view in the application Commented Oct 28, 2019 at 5:36

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

4 Comments

this makes sense but how would i add the link in this?
@kjamp you can simply add the link inside your *ngFor. <a *ngIf="isLast">link here</a>
i'm not getting this to work right.. it isn't showing my list properly
It should work with ...; let islast = last.
4

Try like this:

Working Demo

<ng-container *ngFor="let child of line.children;let i=index">

    <actionmenuitem *ngIf="i != (line.children.length-1)" [line]="child" (inWorkspace)="toWorkspace($event)">
    </actionmenuitem>

    <a [routerLink]="[child]" *ngIf="i == (line.children.length-1)">{{child}}</a>
</ng-container>

2 Comments

Ok this works, but in my list the array keeps repeating.. any way to fix this?
Show your list.
1

you just need to fix last flag assignment:

  • let isLast = last or
  • last as isLast

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.