使用变量将mat-menu元素指定为[matMenuTriggerFor]?

时间:2017-12-25 02:26:01

标签: angular angular-material2

我是一个Angular新手试图使用Material 2实现一个表驱动的级联菜单,但是当我尝试使用变量将mat-menu元素名称的值赋给[matMenuTriggerFor]时会出现运行时错误。我能够找到的所有示例都使用文本字符串来指定mat-menu元素名称;我无法在网络上的任何地方找到帖子,表明其他人都遇到了类似的问题,虽然我确实在github报告上发现了一个月份的帖子,因为绑定到[matMenuTriggerFor]的bug没有用但是已经关闭了作为Material版本4.xx相关的问题。似乎[matMenuTriggerFor]期望表达式的值,但我的尝试不起作用。我做错了什么?

我在Win 10上,我的浏览器是Firefox 57.0.2

My Angular版本:

Angular CLI: 1.5.0
Node: 6.11.3
OS: win32 x64
Angular: 5.1.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 5.0.2
@angular/cli: 1.5.0
@angular/material: 5.0.2
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.0
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.4.2
webpack: 3.8.1

我的app.module.ts:

import { BrowserModule }            from '@angular/platform-browser';
import { NgModule }                 from '@angular/core';
import { BrowserAnimationsModule }  from '@angular/platform-browser/animations';
import { MatMenuModule }            from '@angular/material/menu';
import { MatToolbarModule }         from '@angular/material/toolbar';
import { MatIconModule }            from '@angular/material/icon';
import { MatListModule }            from '@angular/material/list';


import { AppComponent }             from './app.component';
import { NestedMenusComponent }     from './nested-menus/nested-menus.component';

@NgModule({
  declarations: [
    AppComponent,
    NestedMenusComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MatMenuModule,
    MatToolbarModule,
    MatIconModule,
    MatListModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

我的component.ts:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-nested-menus',
  templateUrl: './nested-menus.component.html',
  styleUrls: ['./nested-menus.component.scss']
})
export class NestedMenusComponent implements OnInit {

  constructor() { }

  ngOnInit() { }

  selected :string;
  menuItems = [
    {text: "Tabledriven.Item1" },
    {text: "Tabledriven.Item2", templateName: "submenu"},
  ];

  select(pText :string)
  {
    this.selected = pText;
  }
}

我的component.html:

<div>
  <p>You selected: {{ selected }}</p>

  <div class="demo-menu">
    <div class="menu-section">
      <p>Basic Menu</p>
      <mat-toolbar>
        <button mat-icon-button [matMenuTriggerFor]="basic">
          <mat-icon>more_vert</mat-icon>
        </button>
      </mat-toolbar>

      <mat-menu #basic="matMenu">
        <button mat-menu-item  (click)="select('Basic.Item1')">Basic.Item1</button>
        <button mat-menu-item [matMenuTriggerFor]="submenu">Basic.Item2</button>
      </mat-menu>
    </div>

    <div class="menu-section">
      <p>Table-driven Menu</p>
      <mat-toolbar>
        <button mat-icon-button [matMenuTriggerFor]="tabledriven">
          <mat-icon>more_vert</mat-icon>
        </button>
      </mat-toolbar>

      <mat-menu #tabledriven="matMenu">
        <ng-container *ngFor="let item of menuItems">
          <button *ngIf=!item.templateName mat-menu-item (click)="select(item.text)">{{ item.text }}</button>
          <button *ngIf=item.templateName  mat-menu-item [matMenuTriggerFor]=item.templateName>{{ item.text }}</button>
        </ng-container>
      </mat-menu>
    </div>

    <mat-menu #submenu="matMenu">
      <button mat-menu-item (click)="select('Submenu.Item1')">Submenu.Item1</button>
      <button mat-menu-item (click)="select('Submenu.Item2')">Submenu.Item2</button>
    </mat-menu>
  </div>
</div>

Table-driven菜单在任何用户输入之前抛出运行时错误:

ERROR 
TypeError: this.menu.close is undefined
Stack trace:
MatMenuTrigger.prototype.ngAfterContentInit@webpack-internal:///../../../material/esm5/menu.es5.js:771:9
callProviderLifecycles@webpack-internal:///../../../core/esm5/core.js:12929:9
callElementProvidersLifecycles@webpack-internal:///../../../core/esm5/core.js:12903:13
callLifecycleHooksChildrenFirst@webpack-internal:///../../../core/esm5/core.js:12886:29
checkAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14036:5
callViewAction@webpack-internal:///../../../core/esm5/core.js:14383:21
execEmbeddedViewsAction@webpack-internal:///../../../core/esm5/core.js:14341:17
checkAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14033:5
callViewAction@webpack-internal:///../../../core/esm5/core.js:14383:21
execEmbeddedViewsAction@webpack-internal:///../../../core/esm5/core.js:14341:17
checkAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14033:5
callViewAction@webpack-internal:///../../../core/esm5/core.js:14383:21
execComponentViewsAction@webpack-internal:///../../../core/esm5/core.js:14315:13
checkAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14038:5
callViewAction@webpack-internal:///../../../core/esm5/core.js:14383:21
execComponentViewsAction@webpack-internal:///../../../core/esm5/core.js:14315:13
checkAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14038:5
callWithDebugContext@webpack-internal:///../../../core/esm5/core.js:15286:39
debugCheckAndUpdateView@webpack-internal:///../../../core/esm5/core.js:14823:12
ViewRef_.prototype.detectChanges@webpack-internal:///../../../core/esm5/core.js:11797:13
ApplicationRef.prototype.tick/<@webpack-internal:///../../../core/esm5/core.js:6104:58
ApplicationRef.prototype.tick@webpack-internal:///../../../core/esm5/core.js:6104:13
ApplicationRef.prototype._loadComponent@webpack-internal:///../../../core/esm5/core.js:6170:9
ApplicationRef.prototype.bootstrap@webpack-internal:///../../../core/esm5/core.js:6058:9
PlatformRef.prototype._moduleDoBootstrap/<@webpack-internal:///../../../core/esm5/core.js:5778:74
PlatformRef.prototype._moduleDoBootstrap@webpack-internal:///../../../core/esm5/core.js:5778:13
PlatformRef.prototype.bootstrapModuleFactory/</</<@webpack-internal:///../../../core/esm5/core.js:5699:21
ZoneDelegate.prototype.invoke@webpack-internal:///../../../../zone.js/dist/zone.js:392:17
onInvoke@webpack-internal:///../../../core/esm5/core.js:4949:24
ZoneDelegate.prototype.invoke@webpack-internal:///../../../../zone.js/dist/zone.js:391:17
Zone.prototype.run@webpack-internal:///../../../../zone.js/dist/zone.js:142:24
scheduleResolveOrReject/<@webpack-internal:///../../../../zone.js/dist/zone.js:873:52
ZoneDelegate.prototype.invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:425:17
onInvokeTask@webpack-internal:///../../../core/esm5/core.js:4940:24
ZoneDelegate.prototype.invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:424:17
Zone.prototype.runTask@webpack-internal:///../../../../zone.js/dist/zone.js:192:28
drainMicroTaskQueue@webpack-internal:///../../../../zone.js/dist/zone.js:602:25
NestedMenusComponent.html:29:10
View_NestedMenusComponent_3
NestedMenusComponent.html:29:10
DebugContext_.prototype.logError
core.js:15030
ErrorHandler.prototype.handleError
core.js:1488
ApplicationRef.prototype.tick/<
core.js:5915:54
ZoneDelegate.prototype.invoke
zone.js:392
Zone.prototype.run
zone.js:142
NgZone.prototype.runOutsideAngular
core.js:4701:47
ApplicationRef.prototype.tick
core.js:5915
ApplicationRef.prototype._loadComponent
core.js:5974
ApplicationRef.prototype.bootstrap
core.js:5862
PlatformRef.prototype._moduleDoBootstrap/<
core.js:5582:65
PlatformRef.prototype._moduleDoBootstrap
core.js:5582
PlatformRef.prototype.bootstrapModuleFactory/</</<
core.js:5503
ZoneDelegate.prototype.invoke
zone.js:392
onInvoke
core.js:4753
ZoneDelegate.prototype.invoke
zone.js:391
Zone.prototype.run
zone.js:142
scheduleResolveOrReject/<
zone.js:873
ZoneDelegate.prototype.invokeTask
zone.js:425
onInvokeTask
core.js:4744
ZoneDelegate.prototype.invokeTask
zone.js:424
Zone.prototype.runTask
zone.js:192
drainMicroTaskQueue
zone.js:602
ERROR CONTEXT 
Object { view: {…}, nodeIndex: 2, nodeDef: {…}, elDef: {…}, elView: {…} }
NestedMenusComponent.html:29:10
Angular is running in the development mode. Call enableProdMode() to enable the production mode.
core.js:3660
[WDS] Disconnected!
client:164

“基本”菜单可根据需要使用。打开表驱动菜单时,将显示菜单的两个项目,第二个项目包含指示它显示子菜单但会引发以下运行时错误的图标:

ERROR 
TypeError: templateRef is undefined
 Stack trace:
ViewContainerRef_.prototype.createEmbeddedView@webpack-internal:///../../../core/esm5/core.js:11626:30
DomPortalOutlet.prototype.attachTemplatePortal@webpack-internal:///../../../cdk/esm5/portal.es5.js:448:40
BasePortalOutlet.prototype.attach@webpack-internal:///../../../cdk/esm5/portal.es5.js:303:20
OverlayRef.prototype.attach@webpack-internal:///../../../cdk/esm5/overlay.es5.js:647:45
MatMenuTrigger.prototype.openMenu@webpack-internal:///../../../material/esm5/menu.es5.js:861:13
MatMenuTrigger.prototype._handleClick@webpack-internal:///../../../material/esm5/menu.es5.js:1185:13
View_NestedMenusComponent_3/<@ng:///AppModule/NestedMenusComponent.ngfactory.js:64:23
handleEvent@webpack-internal:///../../../core/esm5/core.js:13777:115
callWithDebugContext@webpack-internal:///../../../core/esm5/core.js:15286:39
debugHandleEvent@webpack-internal:///../../../core/esm5/core.js:14873:12
dispatchEvent@webpack-internal:///../../../core/esm5/core.js:10186:16
renderEventHandlerClosure/<@webpack-internal:///../../../core/esm5/core.js:10807:38
decoratePreventDefault/<@webpack-internal:///../../../platform-browser/esm5/platform-browser.js:2679:53
ZoneDelegate.prototype.invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:425:17
onInvokeTask@webpack-internal:///../../../core/esm5/core.js:4940:24
ZoneDelegate.prototype.invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:424:17
Zone.prototype.runTask@webpack-internal:///../../../../zone.js/dist/zone.js:192:28
ZoneTask.invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:499:24
invokeTask@webpack-internal:///../../../../zone.js/dist/zone.js:1540:9
globalZoneAwareCallback@webpack-internal:///../../../../zone.js/dist/zone.js:1566:17
NestedMenusComponent.html:29:10
ERROR CONTEXT 
Object { view: {…}, nodeIndex: 0, nodeDef: {…}, elDef: {…}, elView: {…} }
NestedMenusComponent.html:29:10

1 个答案:

答案 0 :(得分:1)

您可以动态构建菜单 您只需要保留元素引用,而不是元素名称。

您可以使用@ViewChild@ViewChildren来实现此目的。

以下是您的代码working example