简体   繁体   English

ANGULAR - 打开 mat-menu 时如何保持 select 下拉菜单打开?

[英]ANGULAR - How to keep select dropdown opened when opening mat-menu?

I have a mat-select component that contains custom options (with additional menu "Set as default"):我有一个包含自定义选项的mat-select组件(带有附加菜单“设置为默认值”):

<mat-form-field appearance="fill">
  <mat-label>Favorite food</mat-label>
  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
      <button mat-icon-button [matMenuTriggerFor]="menu" aria-label="Example icon-button with a menu">
        <mat-icon>more_vert</mat-icon>
      </button>
      <mat-menu #menu="matMenu">
        <button mat-menu-item>
          <span>Set as default</span>
        </button>
      </mat-menu>
    </mat-option>
  </mat-select>
</mat-form-field>

However, when I open the menu, select option list disappears and I can see only "Set as default" pop-up.但是,当我打开菜单时,select 选项列表消失了,我只能看到“设置为默认值”弹出窗口。

Can we show "Set as default" on top of the select option list somehow or it is impossible in Angular Material?我们能否以某种方式在 select 选项列表顶部显示“设置为默认值”,或者在 Angular 材料中是不可能的?

This is a link to Stackblitz with the code: https://stackblitz.com/edit/angular-material-playground-hcw1wo?file=src/app/app.component.html这是 Stackblitz 的链接,代码为: https://stackblitz.com/edit/angular-material-playground-hcw1wo?file=src/app/app.component.html

You Can try mat-menu for nested menu items:您可以尝试 mat-menu 嵌套菜单项:

<button mat-raised-button [matMenuTriggerFor]="main_menu">Favorite food</button>
<mat-menu #main_menu="matMenu">
  <ng-container *ngFor="let food of foods">
    <button mat-menu-item [matMenuTriggerFor]="sub_menu">{{ food.value }}</button>
    <mat-menu #sub_menu="matMenu">
       <button mat-menu-item>Set as default</button>
    </mat-menu>
  </ng-container>
</mat-menu>

Or try mat-optgroup of mat-select Reference: https://material.angular.io/components/select/examples或者试试mat-optgroupmat-select optgroup参考: https://material.angular.io/components/select/examples

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

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