如何在mat-menu中有条件地添加[matMenuTriggerFor]

时间:2019-03-26 19:21:42

标签: angular angular-material

#menu菜单项中没有子菜单项,但显示箭头表示有子菜单,这是错误的。 仅当subMenu中有项目时,才如何添加[matMenuTriggerFor] =“ subMenu”。

<mat-menu #menu="matMenu">
    <ng-container *ngFor="let item of menuItem.items; let i=index"> 
        <button class="mat-menu-item" mat-menu-item
            [routerLink]="item.routerLink" routerLinkActive="active" [matMenuTriggerFor]="subMenu"> {{item.name}}
        </button>
        <mat-menu #subMenu="matMenu"> <button class="mat-sub-menu-item"
                *ngFor="let subitem of menuItem.items[i].subitems" mat-menu-item> {{ subitem.name }} </button>
        </mat-menu>
    </ng-container>
</mat-menu>

1 个答案:

答案 0 :(得分:0)

通过在按钮上添加* ngIf =“ menuItem.items [i] .subitems”解决了此问题

<button class="mat-menu-item" [class.more-menu-item]="menuItem.name=='More'"  mat-menu-item  [routerLink]="item.routerLink"
        routerLinkActive="active" [matMenuTriggerFor]="subMenu"><span class="pm-menu-item">{{item.name}}</span><i class="material-icons pm-menu-icon-right" *ngIf="menuItem.items[i].subitems">keyboard_arrow_right</i>
 </button>