How to create dynamic menus in Angular

Viewed 117

I am trying to create dynamic, nested menus in Angular(9), following this link.

I don't know what is missing, getting below errors.

enter image description here

enter image description here

My code in stackblitz. TIA.

1 Answers

The issue here is that menu.childMenu, which is ViewChild, is not initalized yet at the time of passing it to the matMenuTriggerFor directive.

Simple fix is to tell Angular to initialize it before ngOnInit by using static: true option in @ViewChild decorator:

@ViewChild('childMenu', { static: true }) public childMenu;

Forked Stackblitz

Related