meblum
meblum

Reputation: 1944

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?

Upvotes: 4

Views: 10965

Answers (1)

meblum
meblum

Reputation: 1944

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>

Upvotes: 8

Related Questions