Created
June 14, 2019 11:15
-
-
Save eugene-ilyin/706fac220a176eabce8d3f655dd62902 to your computer and use it in GitHub Desktop.
This snippet returns a path of active route in Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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