Get 2024 Templates Mega Bundle!14 Bootstrap, Vue & React Templates + 3 Vector Sets
Get for 99$

Reload Menu


Hi,
I am using metronic angular version 8.2.2. I dynamically load the sidebar-menu but when I refresh my page, the relevant menu is not selected by default. What to do ?


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


Hi,

If you are using a static menu and Angular Router, you can use Angular's routerLinkActive directive to set the active state for a menu item based on the current route.

If you are dealing with a dynamic menu loaded and you still want to highlight the active menu item based on the current route, you can use a combination of Angular's routerLink and routerLinkActive directives along with custom logic in your component to determine the active state. Here's an example:

Assuming you have a dynamic menu loaded from a variable like this:


// your-menu.service.ts
import { Injectable } from "@angular/core";

@Injectable({
providedIn: "root",
})
export class YourMenuService {
getMenuItems(): MenuItem[] {
// Retrieve your dynamic menu items from a variable or API
return [
{ path: "/dashboard", label: "Dashboard" },
{ path: "/profile", label: "Profile" },
// Add more menu items as needed
];
}
}



interface MenuItem {
path: string;
label: string;
}


Now, in your component that renders the dynamic menu:


// your-menu.component.ts
import { Component, OnInit } from "@angular/core";
import { YourMenuService } from "path-to-your-menu-service";
import { ActivatedRoute } from "@angular/router";

@Component({
selector: "app-your-menu",
templateUrl: "./your-menu.component.html",
styleUrls: ["./your-menu.component.css"],
})
export class YourMenuComponent implements OnInit {
menuItems: MenuItem[] = [];

constructor(private menuService: YourMenuService, private route: ActivatedRoute) {}

ngOnInit(): void {
// Load dynamic menu items
this.menuItems = this.menuService.getMenuItems();
}

// Custom logic to determine whether the menu item is active
isMenuItemActive(path: string): boolean {
return this.route.snapshot.routeConfig?.path === path;
}
}


And in your template:


<!-- your-menu.component.html -->
</p><ul>
<li *ngFor="let menuItem of menuItems" [class.active]="isMenuItemActive(menuItem.path)">
<a [routerLink]="menuItem.path">{{ menuItem.label }}</a>

</ul>


In this example, the isMenuItemActive method checks if the current route matches the path associated with the menu item. If it does, the "active" class is added to highlight the menu item.

Ensure you have the appropriate styles for the "active" class in your CSS as mentioned in the previous response.

Adjust the code according to your specific use case and styling preferences. This approach should work for dynamically loaded menus based on a variable.

@ geometry dash scratch: Thanks for your instruction. This is what I am looking for.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(