本文介绍了带有angular1之类的嵌套下拉选项的材质设计angular2/4菜单的示例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很想加入angular2材质,注意以下菜单示例:

I really want to incorporate angular2 material, noticed the following menu example:

https://material.angularjs.org/1.1.4/demo/menuBar

这很美: http://codepen.io/anon/pen/zrdQwP

我喜欢如何使用嵌套菜单,但是对于Angular 2/4演示,您所看到的就是:

I like how you can have nested menus, but for the Angular 2/4 demo, all you see is this:

https://material.angular.io/components/component/menu

没有嵌套菜单的示例! Angular2 + Material可以做到吗?如果是这样,有人可以演示如何使用它吗?还是这不可能?如果Angular 2/4无法支持此功能,而Angular 1.x可以支持,则似乎很麻烦.

There is no example with nested menus! Is this possible with Angular2 + Material? If so, can someone demonstrate how to use this? Or is this just not possible? Seems fishy if Angular 2/4 can't support this while Angular 1.x can...

推荐答案

更新:

此功能已添加到 2.0.0-beta.10 découpage-panjandrum

This feature was added in 2.0.0-beta.10 découpage-panjandrum

示例

Example

以前的版本:

计划在将来 https://github.com/angular/material2/issues/1429

但是现在我怀疑这是可能的.这是我的 Plunker Material2菜单示例 ,其中展示了这样的菜单

But now i suspect it is possible. Here is my Plunker Material2 Menu Example that demonstrates such menu

模板可能如下所示:

Template might look like this:

<ng-container *ngFor="let item of items">
  <button md-button [mdMenuTriggerFor]="menu" #trigger="mdMenuTrigger" [attr.data-level]="1" (mouseenter)="openMenu(trigger, 1)">
    {{item.text}}
  </button>
  <md-menu class="custom-menu" #menu="mdMenu" [overlapTrigger]="false" (close)="closeMenu()" xPosition="after">
    <ng-container *ngTemplateOutlet="subMenu; context: { $implicit: item.items, level: 2 }"></ng-container>
  </md-menu>
</ng-container>

<ng-template #subMenu let-items let-level="level">
  <ng-container *ngFor="let item of items">
    <ng-container *ngIf="item.items && item.items.length else simpleTmpl">
      <button *ngIf="!item.divider" md-menu-item [disabled]="item.disabled"
              [mdMenuTriggerFor]="menu"
              #trigger="mdMenuTrigger"
              [attr.data-level]="level"
              (mouseenter)="openMenu(trigger, level);" (click)="$event.stopPropagation()">
        <span>{{item.text}}</span>
        <span *ngIf="item.extraText">{{item.extraText}}</span>
        <md-icon *ngIf="item.icon">{{item.icon}}</md-icon>
      </button>
      <md-menu class="sub-menu" #menu="mdMenu" [overlapTrigger]="false" xPosition="before" >
        <ng-container *ngTemplateOutlet="subMenu; context: { $implicit: item.items || [], level: level + 1 }"></ng-container>
      </md-menu>
      <md-divider *ngIf="item.divider" class="mat-divider"></md-divider>
    </ng-container>
    <ng-template #simpleTmpl>
      <button *ngIf="!item.divider" md-menu-item [disabled]="item.disabled" (click)="closeMenu()">
        <span>{{item.text}}</span>
        <span *ngIf="item.extraText">{{item.extraText}}</span>
      </button>
      <md-divider *ngIf="item.divider" class="mat-divider"></md-divider>
    </ng-template>
  </ng-container>
</ng-template>

您只需要声明项目定义:

You need only to declare items definitions:

menuItems = [
  {
    text: 'File',
    items: [
      {
        text: 'Share'
      },
      {
        text: 'New',
        icon: 'arrow_right',
        items: [
          {
            text: 'Document'
          },
          {
            text: 'Form'
          }
        ]
      },
      {
        text: 'Print',
        extraText: 'Ctrl+P'
      }
    ]
  },
  {
    text: 'Edit',
  }
];

这篇关于带有angular1之类的嵌套下拉选项的材质设计angular2/4菜单的示例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:04