我有一个菜单项列表,我想将数组中的最后一个项目链接。
现在菜单项是从组件构建的,但是我不确定如何使数组中的最后一项成为链接。
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);
}
尝试这样:
<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>
Angular公开了您可以使用的以下变量:
因此,使最后一项成为链接,您可以执行此操作
<div *ngFor="let child of line.children; let last = islast">
<actionmenuitem *ngIf="islast" [line]="child"
(inWorkspace)="toWorkspace($event)">
</actionmenuitem>
</div>