简体   繁体   English

如何打开/关闭角度材质菜单

[英]How to open/close a angular-material menu

I recently started using angular-material and am struggling/unsure about opening/closing a mat-menu... I see in the examples on the angular-material documentation site that they assign an id to the menu and then apply a directive to the button that is used to toggle the menu. 我最近开始使用角度材料,我正在努力/不确定打开/关闭mat菜单......我在角度材料文档网站的示例中看到,他们为菜单分配了一个id,然后将指令应用于用于切换菜单的按钮。 eg [matMenuTriggerFor]="menu" 例如[matMenuTriggerFor]="menu"

How can I go about writing a directive that does that? 我怎样才能编写一个指令呢? I'm not sure how to pass a reference to a specific menu to a directive that then calls the toggle() method on the DOM element with that id? 我不确定如何将对特定菜单的引用传递给指令,该指令随后使用该id调用DOM元素上的toggle()方法?

The following code produces the errors: 以下代码生成错误:

Can't bind to 'matMenuTriggerFor' since it isn't a known property of 'button'. 无法绑定到'matMenuTriggerFor',因为它不是'button'的已知属性。

There is no directive with "exportAs" set to "matMenu". “exportAs”设置为“matMenu”没有指令。
My code: 我的代码:

<li>
    <button mat-icon-button [matMenuTriggerFor]="stockSystemMenu">
        <mat-icon class="sn-item">
            <i class="material-icons">archive</i>
        </mat-icon>
    </button>
    <span class="sn-item" (click)="toggleMenu(stockSystemMenu)">Stok System</span>

    <mat-menu #stockSystemMenu="matMenu">
        <button mat-menu-item>
            <mat-icon>
                <i class="material-icons">chevron_right</i>
            </mat-icon>
        <span>Service 1</span>
        </button>
    </mat-menu>
</li>

There is confusion because Material introduced a breaking change as I understand it.. See material 2 Changelog - Breaking Changes 有一些混乱,因为材料引入了一个突破性的变化,因为我理解它。见材料2变更日志 - 突破变化

Starting with Material 2.0.0-beta.12. 从Material 2.0.0-beta.12开始。 Use mat instead of md-* .. Seems only some of the docs at material.angular.io are updated with mat. 使用mat而不是md-* ..似乎只有material.angular.io中的一些文档用mat更新。 Specifically, if you click view source and see md , I believe they have yet to replace it with mat . 具体来说,如果你点击查看源并看到md ,我相信他们还没mat替换它。

So either update to Material 2.0.0-beta.12 and use mat-* , or use md-* . 因此要么更新到Material 2.0.0-beta.12并使用mat- mat-* ,要么使用md-*

"Your code is correct, you don't need to write matMenuTriggerFor directive, it is part of the API, make sure you have imported the MatMenuModule, MatButtonModule, and MatIconModule into your app module." “您的代码是正确的,您不需要编写matMenuTriggerFor指令,它是API的一部分,请确保已将MatMenuModule,MatButtonModule和MatIconModule导入到您的应用程序模块中。” - from comments - 来自评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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