I am working on an angular app with a top menu and i need to set the menu active based on the url. Here is what i have tried.
URL example
http://localhost:4200/home/about - About should be highlighted
http://localhost:4200/home/service/supply - Service should be highlighted
http://localhost:4200/home/contact/details- Contact should be highlighted
if (event instanceof NavigationStart) {
this.currentMenu = this.elem.nativeElement.querySelectorAll('.nav-link');
for (let i = 0; i < this.currentMenu.length; i++) {
if(event.url.indexOf('/about') > -1){
if(this.currentMenu[i].innerHTML == "About"){
this.currentMenu[i].style.color = "red";
}
} else if (event.url.indexOf('/service') > -1){
if(this.currentMenu[i].innerHTML == "Service"){
this.currentMenu[i].style.color = "red";
}
}
else if (event.url.indexOf('/contact') > -1){
if(this.currentMenu[i].innerHTML == "Contact"){
this.currentMenu[i].style.color = "red";
}
}
}
}
})
I am not sure whether this is the right approach to achieve this. any inputs will be highly appreciated