admin管理员组文章数量:1296873
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"
}
]
}
]
}
]
}
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"
}
]
}
]
}
]
}
Share
edited Jun 17, 2021 at 18:01
Sha
asked Jun 17, 2021 at 17:39
ShaSha
1,9746 gold badges35 silver badges60 bronze badges
2 Answers
Reset to default 9You can use routerLink
Use this directive to create a visual distinction for elements associated with an active route. For example, the following code highlights the word "Bob" when the router activates the associated route:
<a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>
Whenever the URL is either '/user' or '/user/bob', the "active-link" class is added to the anchor tag. If the URL changes, the class is removed.
You can set more than one class using a space-separated string or an array. For example:
<a routerLink="/user/bob" routerLinkActive="class1 class2">Bob</a>
<a routerLink="/user/bob" [routerLinkActive]="['class1', 'class2']">Bob</a>
To add the classes only when the URL matches the link exactly, add the option exact: true:
<a routerLink="/user/bob" routerLinkActive="active-link" [routerLinkActiveOptions]="{exact:
true}">Bob</a>
To directly check the isActive status of the link, assign the RouterLinkActive instance to a template variable. For example, the following checks the status without assigning any CSS classes:
<a routerLink="/user/bob" routerLinkActive #rla="routerLinkActive">
Bob {{ rla.isActive ? '(already open)' : ''}}
</a>
You can apply the RouterLinkActive directive to an ancestor of linked elements. For example, the following sets the active-link class on the parent tag when the URL is either '/user/jim' or '/user/bob'.
<div routerLinkActive="active-link" [routerLinkActiveOptions]="{exact: true}">
<a routerLink="/user/jim">Jim</a>
<a routerLink="/user/bob">Bob</a>
</div>
You can use ngClass.
<div
[ngClass]="'active': condition === true"
>
</div>
this will assign the active class to your div upon a successful condition
本文标签: javascriptMake menu active in angularStack Overflow
版权声明:本文标题:javascript - Make menu active in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741646328a2390204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论