I'm using angular 13, and i have to load a menu dynamically from Database so i have a function doing that name's loadMenu() , and i have to put the sidebare (where i have to put the menu loaded) in MetisMenu, but the problem is MetisMenu didn't work correcly without settimeout()
this is my code :
ngOnInit(): void {
this.LoadMenu();
}
LoadMenu() {
if (this.sessionManager.context != null) {
let arg = new P();
this.service.getMenu(arg).subscribe(
data => {
this._menu = data;
},
error => {
console.log(error);
}
);
}}
ngAfterViewInit() {
setTimeout(() => {
new MetisMenu(this.sidebarMenu.nativeElement);
this._activateMenuDropdown();
}, 1000);}
Explaining what's happening: The problem is that LoadMenu() is asynchronous.
So when you try activate MetisMenu, menu's data doesn't even exists yet. When you setTimeout by 1000ms, the asyncrounous function has enought time to end its process. That's why it works.
You should do it like so: