问题
I have the following html:
<button mat-icon-button #notificationMenuBtn [matMenuTriggerFor]="notificationsMenu">
</button>
<mat-menu #notificationsMenu="matMenu" [overlapTrigger]="false">
</mat-menu>
How to I access thematMenuTriggerFor on the notificaitonMenuBtn from typescript? I tried using a View Child (shown below) but I can't seem to bind it to the trigger, only to the button.
@ViewChild('notificationMenuBtn') notificationMenuBtn : MatMenuTrigger;
this.notificationMenuBtn.openMenu();
回答1:
If you really need to use an id (When you have several MatMenuTrigger)
@ViewChild('notificationMenuBtn', {read: MatMenuTrigger}) protected notificationMenuBtn : MatMenuTrigger;
回答2:
Figured it out. I just needed to use MatMenuTrigger instead of the element id.
@ViewChild(MatMenuTrigger) notificationMenuBtn: MatMenuTrigger;
来源:https://stackoverflow.com/questions/47080338/how-do-i-access-mat-menu-trigger-from-typescript