javascript - Angular 7 - 使用 ngFor 将数组中的最后一项设为链接
问题描述
我有一个菜单项列表,我想将数组中的最后一项设为链接。
现在菜单项是从一个组件构建的,但我不确定如何使数组中的最后一项成为链接。
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>
推荐阅读
- java - 如何在 mockito 中模拟 servlet 上下文
- android - 奥利奥本地化有时不起作用
- jsp - 如何从 AEM 的 Java 文件中获取给定再现的资产路径?
- uitableview - UITableView 没有出现在 iPhone 中
- c# - c# Naaudio音频电平捕获和显示,仅在打开录音属性时有效
- javascript - 多语言验证处理逻辑
- javascript - 如何动态更改 logoURL?
- python - 在python中对不同的列进行分组
- jquery - 如何通过下拉列表的变化根据数据属性对div进行排序
- qt - Qt QTreeView indexBelow 不起作用