简体   繁体   English

Angular Material - mat-menu 的动态样式

[英]Angular Material - dynamic style for mat-menu

I would like to apply a dynamic style to the content of mat-menu.我想对 mat-menu 的内容应用动态样式。 I know that I can use panelClass to assign a class, but my class is dynamic.我知道我可以使用panelClass来分配一个类,但我的类是动态的。

Angular has a [ngStyle] or just [style.attribute] binding for such cases, but that does not work on the mat-menu (or other overlays), it only works on directly rendered elements.对于这种情况,Angular 具有[ngStyle]或仅[style.attribute]绑定,但这不适用于 mat-menu(或其他叠加层),它仅适用于直接渲染的元素。

I am looking for something like panelStyle which would allow me to set the styles dynamically directly on the panel which holds the mat-menu.我正在寻找像panelStyle这样的东西,它允许我直接在包含 mat-menu 的面板上动态设置样式。

Here is a code example, where panelClass allows me to set some css, but only static one and ngStyle is useless.这是一个代码示例,其中 panelClass 允许我设置一些 css,但只有静态一个,ngStyle 没用。

<mat-menu [ngStyle]="{'background-color': colorVariable }" panelClass="some-static-class-works">

What I am looking for:我在找什么:

<mat-menu [panelStyle]="{'background-color': colorVariable }">

You can wrap your menu content in a single DIV and apply style dynamically to that.您可以将菜单内容包装在单个 DIV 中,并对其动态应用样式。 With background-color, to get it to fill the entire panel you need to adjust margins and padding.使用背景颜色,要让它填满整个面板,您需要调整边距和填充。 For example:例如:

<mat-menu>
  <div [ngStyle]="{'background-color': colorVariable }" class="menu-panel">
    <button mat-menu-item>Item 1</button>
    <button mat-menu-item>Item 2</button>
  </div>
</mat-menu>

.menu-panel {
  margin: -8px 0; 
  padding: 8px 0;
}

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

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