Skip to content

Instantly share code, notes, and snippets.

@Mattnmoore
Last active October 22, 2018 17:19
Show Gist options
  • Select an option

  • Save Mattnmoore/df827b036f240a2b471420a67958912f to your computer and use it in GitHub Desktop.

Select an option

Save Mattnmoore/df827b036f240a2b471420a67958912f to your computer and use it in GitHub Desktop.
Angular Router Titles
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;
}
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