Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the selected item from angular material `mat-menu`

I'm using an angular material menu component

<button mat-button [matMenuTriggerFor]="menu">Menu</button>
<mat-menu #menu="matMenu">
  <button mat-menu-item>Item 1</button>
  <button mat-menu-item>Item 2</button>
</mat-menu>

How do I listen for selection changes? Is there a way to check which item the user selected without listening on each button separately for the click event?

like image 222
meblum Avatar asked Oct 26 '25 02:10

meblum


1 Answers

The best way would be using a loop. Something like:

    const options = ['option1','option2'];

    <button mat-button color="primary" [matMenuTriggerFor]="menu">Copy</button>

    <mat-menu #menu="matMenu">
            <button (click)="onClick(option)" *ngFor="let option of options" mat-menu-item>
                {{option}}
            </button>
    </mat-menu>
like image 50
meblum Avatar answered Oct 27 '25 16:10

meblum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!