简体   繁体   中英

The data is not rendering in the mat-menu

I been trying to create a navbar with a submenu. The nav bar data is rendered successfully but the submenu is not working, I am very new to this. Please let me know where is the problem.

  <mat-nav-list class="navigation relative">
    <div *ngFor="let link of navLinks; let i=index" style="padding-bottom: 14px;">
        <div class="card">
            <mat-list-item appAccordionlinks routerLinkActive="active-link" (click)="onClick(link.name)">
                <a mat-button [matMenuTriggerFor]="submenu" *ngIf="link?.submenu?.length > 0" appAccordiontoggle
                    class="relative main-active">
                    <mat-icon *ngIf="link.icon">{{link.icon}}</mat-icon>
                    <img class="menu-img" *ngIf="link.img" [src]="link.img" alt="">
                    <app-menu-icon *ngIf="link.img" [name]="'employee'"></app-menu-icon>
                    <span>{{link.name}}</span><span fxFlex></span>
                </a>

                <a *ngIf="!(link?.submenu?.length > 0)" [routerLink]="link.path" appAccordiontoggle
                    class="relative main-active" mat-ripple fxLayout="row">
                    <mat-icon *ngIf="link.icon">{{link.icon}}</mat-icon>
                    <app-menu-icon *ngIf="link.img" [name]="link.name" [color]="link.color"></app-menu-icon>

                    <!-- <img class="menu-img" *ngIf="link.img" [src]="link.img" alt=""> this is need to mark as comment -->
                    <span>{{link.name}}</span>
                </a>
            </mat-list-item>
        </div>
    </div>
</mat-nav-list>

<mat-menu #submenu>
    <ng-container *ngFor="let sublink of link?.submenu">
        <ng-container *ngIf="link?.submenu?.length > 0">
            <button mat-menu-item>
                <img class="menu-img" *ngIf="sublink.img" [src]="sublink.img">
                <span>{{sublink.name}}</span>
            </button>
        </ng-container>
    </ng-container>
</mat-menu>

The mat-menu element is out of the scope of link variable. You need to include it ( mat-menu ) in the div you are dynamically rendering the links, ie within link 's scope:

<div *ngFor="let link of navLinks; let i = index"...>
  <--! card html -->
  <mat-menu #submenu>
    <--! submenu child elements... -->
  </mat-menu>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM