Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep mat-menu open angular

I am trying to use checkbox options in a menu, but I need to keep the menu open until the user is finished selecting options. I am using the latest version of Angular. Thank you in advance!

enter image description here

I have combined the nested mat-menu from here with checkboxes:

What I have tried, but the menu closes after a checkbox is selected:

<mat-menu #worldtest="matMenu" md-prevent-menu-close="md-prevent-menu-close">
     <section><mat-checkbox class="example-margin" [(ngModel)]="checked">Checked</mat-checkbox></section>
     <section><mat-checkbox class="example-margin" [(ngModel)]="indeterminate">Indeterminate</mat-checkbox></section>
</mat-menu>
like image 216
dataviews Avatar asked Jun 29 '18 14:06

dataviews


People also ask

What is mat menu?

mat-menu selector is used to display floating menu panel containing list of menu items.

How do I create a menu in angular 10?

By adding #menu=”mdMenu” to the element the menu template variable is created. The menu consists of three menu items. Each item is added to the template by using a button element and adding the md-menu-item directive to the button element. The md-icon element is used to display an icon next to the menu item text.


1 Answers

When you use a checkbox (or any interactive element that responds to clicks) on a mat-menu, and you don't want the menu to close when you click it, you need to prevent the menu from receiving the click event by stopping it with MouseEvent.stopPropagation(). Try something like this:

<mat-checkbox (click)="$event.stopPropagation()">Check Me</mat-checkbox>

The md-prevent-menu-close feature is from the old Angular Material for AngularJS - the 'latest version' of Angular Material (v6) doesn't have that feature. Angular Material (v1.x for AngularJS) and Angular Material2 (v2/5/6 for Angular) are not interchangeable.

like image 107
G. Tranter Avatar answered Oct 28 '22 00:10

G. Tranter