One approach is to implement route guards. The canActivate
guard sets a flag to true, while the canDeactivate
guard sets it to false. This flag can then be bound to the navigation item.
Another option is to utilize a routing event watcher that monitors specific events and toggles a flag accordingly, which can also be bound to the desired element.
In the following example, routing events are used to control a spinner by toggling a flag:
constructor(private authService: AuthService,
private router: Router,
private messageService: MessageService) {
router.events.subscribe((routerEvent: Event) => {
this.checkRouterEvent(routerEvent);
});
}
checkRouterEvent(routerEvent: Event): void {
if (routerEvent instanceof NavigationStart) {
this.loading = true;
}
if (routerEvent instanceof NavigationEnd ||
routerEvent instanceof NavigationCancel ||
routerEvent instanceof NavigationError) {
this.loading = false;
}
}
A similar approach could involve monitoring NavigationStart events for specific routes. For instance, you could try something like this (not tested):
constructor(private authService: AuthService,
private router: Router,
private messageService: MessageService) {
router.events.subscribe((routerEvent: Event) => {
this.checkRouterEvent(routerEvent, router.url);
});
}
checkRouterEvent(routerEvent: Event, routerUrl: string): void {
if (routerEvent instanceof NavigationStart && routerUrl == '/item') {
this.myflag= true;
}
if (routerEvent instanceof NavigationEnd ||
routerEvent instanceof NavigationCancel ||
routerEvent instanceof NavigationError) {
this.myflag= false;
}
}