-
-
Save ikishanoza/bbe0f89baf61a00538582c9ec1c09461 to your computer and use it in GitHub Desktop.
Dynamically set page title based on active route in Angular 4
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
import ... | |
@NgModule({ | |
... | |
providers: [ TitleService ], | |
}) | |
export class AppModule { | |
constructor(titleService: TitleService) { | |
titleService.init(); | |
} | |
} |
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
// If a route is declared with a title attribute, it gets used, otherwise we just try to parse the url fragment | |
// Here's where the title attribute goes | |
const routes: Routes = [{ | |
path: '', | |
component: HomeComponent, | |
data: {title: "My Home Page"}, | |
}, { | |
path: 'detail/:id', | |
component: DetailComponent, | |
}]; | |
@NgModule({ | |
imports: [RouterModule.forChild(routes)], | |
exports: [RouterModule], | |
}) | |
export class ExampleRoutingModule { | |
} |
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
// Credits and thanks: | |
// https://toddmotto.com/dynamic-page-titles-angular-2-router-events | |
// https://stackoverflow.com/questions/34597835/how-to-get-current-route | |
// | |
import { Injectable } from '@angular/core'; | |
import { Title } from '@angular/platform-browser'; | |
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'; | |
import { filter, map, switchMap } from 'rxjs/operators'; | |
const APP_TITLE = 'NoDice!'; | |
const SEPARATOR = ' > '; | |
@Injectable() | |
export class TitleService { | |
constructor( | |
private router: Router, | |
private activatedRoute: ActivatedRoute, | |
private titleService: Title, | |
) {} | |
init() { | |
this.router.events.pipe( | |
filter(event => event instanceof NavigationEnd), | |
map(() => this.activatedRoute), | |
map(route => route.firstChild), | |
switchMap(route => route.data), | |
map((data) => { | |
if (data.title) { | |
// If a route has a title set (e.g. data: {title: "Foo"}) then we use it | |
return data.title; | |
} else { | |
// If not, we do a little magic on the url to create an approximation | |
return this.router.url.split('/').reduce((acc, frag) => { | |
if (acc && frag) { acc += SEPARATOR; } | |
return this.router.url.split('/').reduce((acc, frag) => { | |
if ( acc && frag ) { acc += SEPARATOR; } | |
return acc + TitleService.ucFirst(frag); | |
}); | |
}); | |
} | |
}) | |
) | |
.subscribe((pathString) => this.titleService.setTitle(`${APP_TITLE} ${pathString}`)); | |
} | |
static ucFirst(string) { | |
if ( !string ) { return string; } | |
return string.charAt(0).toUpperCase() + string.slice(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment