Last active
October 22, 2018 17:19
-
-
Save Mattnmoore/df827b036f240a2b471420a67958912f to your computer and use it in GitHub Desktop.
Angular Router Titles
This file contains hidden or 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 route: ActivatedRoute | |
| private titleService: Title) { | |
| router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => { | |
| let title = this.getTitleFromRoute(route.root) + ' - Dinolytics'; | |
| this.titleService.setTitle(title); | |
| }); | |
| } | |
| private getTitleFromRoute(route) { | |
| if (route.snapshot.data.hasOwnProperty('title')) { | |
| return route.snapshot.data.title; | |
| } | |
| let children: ActivatedRoute[] = route.children; | |
| // iterate over route children | |
| for (let child of children) { | |
| // verify that we have the correct router outlet | |
| if (child.outlet !== PRIMARY_OUTLET) { | |
| continue; | |
| } | |
| if ( ! child.snapshot.data.hasOwnProperty('title')) { | |
| return this.getTitleFromRoute(child); | |
| } | |
| return child.snapshot.data.title; | |
| } | |
| return false; | |
| } |
This file contains hidden or 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
| export const routes = [{ | |
| path: ':id', | |
| component: GroupsEditComponent, | |
| data: { | |
| title: 'Edit Group' | |
| }, | |
| }] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment