I try to add an underline on the active-item in my NavBar component by binding the return value of the isActive() function. isActive() compares the url path with the string value passed in and returns true or false.
When I click on either item in the NavBar component, the isActive() function runs and the underline displays as expected on the NavBar element that corresponds to the page I navigated to.
However when I click an anchor tag outside of the NavBar component, the isActive() function doesn't get run again, so the underline remains on the navbar element for the previous page:
NavBar HTML:
<div class="anchor-wrapper" [class.active-item]="isActive('/dashboard')">
<a [routerLink]="['/dashboard']">Dashboard</a>
</div>
<div class="anchor-wrapper" [class.active-item]="isActive('/inventory')">
<a [routerLink]="['/inventory']">Inventory</a>
</div>
NavBar TS:
isActive(path) {
return (url.parse(window.location.href).pathname === path) ? true : false;
}
Dashboard HTML:
<a (click)="onVehicleStatusClick()" RouterLinkActive="active"> {{promotedCount}} </a>
Dashboard TS:
onVehicleStatusClick(){
this.router.navigate(['/inventory']);
}
How do I ensure that isActive() runs again when I navigate to another route outside of the NavBar component?
Many thanks from an Angular newbie
