简体   繁体   中英

angular2-mdl dynamic drop-down menu

I am developing an application using angular2. Currently i am trying to find out how to make a dynamic drop-down menu using angular2-mdl but I can't seem to find a correct solution for it. here is a code snippet from my project

<nav class="mdl-navigation mdl-layout--large-screen-only">

  <ng-container *ngFor="let link of links; let i = index">

    <ng-container *ngIf="!link.hasSubLinks()">

      <button mdl-button class="mdl-navigation__link" (click)="route(link.url)" [routerLink]="link.url" routerLinkActive="active">
        {{link.name}}
      </button>

    </ng-container>

    <ng-container *ngIf="link.hasSubLinks()">

      <button mdl-button class="mdl-navigation__link" #{{link.name}}="mdlButton" [mdl-toggle-menu]="submenu">
        {{link.name}}
      </button>

      <mdl-menu #submenu="mdlMenu" mdl-menu-position="bottom-right" class="mdl-color--blue-grey-800">

        <mdl-menu-item *ngFor="let subLink of link.subLinks">
          <a class="mdl-navigation__link" [routerLink]="subLink.url" routerLinkActive="active">

        <mdl-icon mdl-list-item-icon [mdl-badge]="subLink.badge>0?subLink.badge:null" mdl-badge-overlap role="presentation" class="colorwhite">{{subLink.icon}}</mdl-icon>{{subLink.name}}

          </a>

        </mdl-menu-item>

      </mdl-menu>

    </ng-container>

  </ng-container>

</nav>

It seems its not working the way i expect it to be like in material design lite page regarding menu , where when i click one menu the other closes. What happens is I can open all the drop-down menus but when I try to close one, all of them close. I think my problem is the <button ... [mdl-toggle-menu]="submenu"> and the <mdl-menu #submenu="mdlMenu" ... > is there a way to dynamically set the values for it?

I tried changing the values to <button ... [mdl-toggle-menu]="link.icon"> and <mdl-menu #{{link.icon}}="mdlMenu" ... > but i get an error. Any ideas?

error_handler.js:54 TypeError: this.menu.toggle is not a function
at MdlToggleMenuDirective.onClick (mdl-toggle-menu.directive.js:11)
at _View_AppComponent3._handle_click_2_2 (component.ngfactory.js:1162)
at view.js:375
at dom_renderer.js:262
at dom_events.js:30
at ZoneDelegate.invoke (zone.js:232)
at Object.onInvoke (ng_zone.js:238)
at ZoneDelegate.invoke (zone.js:231)
at Zone.runGuarded (zone.js:128)
at NgZone.runGuarded (ng_zone.js:133)

EDIT

thanks for the template... here is the plunker code http://plnkr.co/edit/R6Bnadu124qOohjUWuMZ?p=preview

You are doing everything right. There is a bug in angular2-mdl: an open menu will not be closed if another menu will be opened (just created: https://github.com/mseemann/angular2-mdl/issues/200 ).

The workaround until this is fixed: register a click event listener on every button that opens a menu:

<button mdl-button (click)="hideAllExcept(submenu)" class="mdl-navigation__link" #{{link.name}}="mdlButton"  [mdl-toggle-menu]="submenu">{{link.name}}</button>

query in your app component for all menuComponents:

@ViewChildren(MdlMenuComponent) private menuComponents: QueryList<MdlMenuComponent>;

and hide all menus except the one that will be opened:

  hideAllExcept(submenu) {
   this.menuComponents.forEach( (menu) => {
     if (submenu !== menu) {
       menu.hide();
     }
   });
  }

Here is a plnkr with the workaround: http://plnkr.co/edit/Yo7F1HKZSmVU309selTc?p=info

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