How do I access mat menu trigger from typescript

拜拜、爱过 提交于 2019-12-10 17:09:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!