Skip to content

Instantly share code, notes, and snippets.

@eugene-ilyin
Created June 14, 2019 11:15
Show Gist options
  • Save eugene-ilyin/706fac220a176eabce8d3f655dd62902 to your computer and use it in GitHub Desktop.
Save eugene-ilyin/706fac220a176eabce8d3f655dd62902 to your computer and use it in GitHub Desktop.
This snippet returns a path of active route in Angular
constructor(private router: Router,
private actRoute: ActivatedRoute) { }
...
this.router.events.filter(event => event instanceof NavigationEnd).subscribe((event) => {
const getActiveRoutePath = (routeSnapshot: ActivatedRouteSnapshot) => {
if (routeSnapshot.routeConfig) {
// Check top level route.
return routeSnapshot.routeConfig.path;
} else {
// Check childrens of this route.
for (let i = 0; i < routeSnapshot.children.length; i++) {
if (routeSnapshot.children[i] instanceof ActivatedRouteSnapshot) {
return getActiveRoutePath(routeSnapshot.children[i]);
}
}
}
};
// Returns path of an activated route, for example 'song/:songId'
console.log(getActiveRoutePath(this.actRoute.snapshot))
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment