Home > Software engineering >  I want it to open automatically in my matmenu when the page is opened
I want it to open automatically in my matmenu when the page is opened

Time:09-11

I want my material menu to open automatically when my page is opened, but because I am not click, "trigger" is undefined

TypeError: Cannot read properties of undefined (reading 'openMenu')

 @ViewChild('menuTrigger') trigger;
    ngOnInit() {  
    if (this.headerService.isCookieDown = "0") {
          this.trigger.openMenu()
        } 
    }



 <button mat-button #menuTrigger="matMenuTrigger" [matMenuTriggerFor]="infomenu"><img bell.svg"></button>



 <mat-menu  style="max-width: 400px !important;" [hasBackdrop]="false" #infomenu="matMenu"> 
html codes..
 </mat-menu>

CodePudding user response:

Implement the ngAfterViewInit lifecycle hook and make the openMenu call in ngAfterViewInit, like below.

ngAfterViewInit() {  
    if (this.headerService.isCookieDown = "0") {
      this.trigger.openMenu()
    } 
}
  • Related