最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Which mat-menu was selected? - Stack Overflow

matteradmin6PV0评论

The Angular Material documentation shows us the markup for mat-menu:

<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>
    <mat-icon>dialpad</mat-icon>
    <span>Redial</span>
  </button>
  <button mat-menu-item disabled>
    <mat-icon>voicemail</mat-icon>
    <span>Check voice mail</span>
  </button>
</mat-menu>

But does not show how to capture which mat-menu-item was selected. How do we do this?

The Angular Material documentation shows us the markup for mat-menu:

<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>
    <mat-icon>dialpad</mat-icon>
    <span>Redial</span>
  </button>
  <button mat-menu-item disabled>
    <mat-icon>voicemail</mat-icon>
    <span>Check voice mail</span>
  </button>
</mat-menu>

But does not show how to capture which mat-menu-item was selected. How do we do this?

Share Improve this question asked Dec 16, 2019 at 15:57 OleOle 47.6k70 gold badges238 silver badges447 bronze badges 2
  • what do you want to achieve? – Lia Commented Dec 16, 2019 at 16:12
  • Just needed to identify which menu item was selected in the even handler. – Ole Commented Dec 16, 2019 at 19:57
Add a ment  | 

1 Answer 1

Reset to default 6

I have not found it in Angular documentation, but as you have used button in mat-menu-item, you can use its click event to capture the selected value like below

<button mat-menu-item (click)="selectedMenu('Redial')">
    <mat-icon>dialpad</mat-icon>
    <span>Redial</span>
  </button>
Post a comment

comment list (0)

  1. No comments so far