In my application i have a header with menu items in it. i am retrieving the menu list from a service and displaying the same in the header. on hover of the main list i am displaying the sub menus. i want to make my parent item active when its child item is clicked and navigate to its page(child item can be at multiple levels). I want the user to be notified under which page they are in.
HTML
<div class="collapse navbar-collapse">
<ul class="navbar-nav mx-auto">
<li class="nav-item dropdown" *ngFor="let menu of menus"></li>
<a class="nav-link dropdown-toggle" href="#" id="{{ menu.label }}" data-toggle="dropdown"
aria-expanded="false">{{ menu.label }}</a>
<div *ngIf="menu.items.length > 0" class="dropdown-menu mt-0">
<span *ngFor="let item of menu.items | SortOptionsBy: 'label'">
<a routerLink="{{ item.url }}" *ngIf="!item.disabled" class="dropdown-item">
{{ item.label }}
</a>
<span *ngIf="item.items && item.items.length > 0">
<span *ngFor="let submenu of item.items | SortOptionsBy: 'label'">
<a href="{{ submenu.url }}" class="dropdown-item" target="_blank">
{{ submenu.label }}
</a>
</span>
</span>
</span>
</div>
</ul>
</div>
TS file
getMenus() {
this.subscription = this.navService.getMenuList().subscribe(data => {
this.menus = data;
});
}
Sample response
{
"data": [
{
"label": "Services",
"items": [
{
"label": "Lawn maintainance",
"url": "/home/lawn-maintainance"
},
{
"label": "Pool Cleaning",
"url": "/home/services/pool-cleaning"
}
]
},
{
"label": "Contact",
"items": [
{
"label": "Conatct Supplier",
"url": "/home/contact/contact-supplier"
},
{
"label": "Place Order",
"url": "/home/contact/place-order",
"items": [
{
"label": "email",
"url": "/home/contact/contact-supplier/email"
},
{
"label": "phone",
"url": "/home/contact/contact-supplier/phoe"
}
]
}
]
}
]
}