11import { Component , ViewEncapsulation } from '@angular/core' ;
22import { Router , NavigationStart } from '@angular/router' ;
3+ import { Title } from '@angular/platform-browser' ;
34
45
56@Component ( {
@@ -10,8 +11,9 @@ import {Router, NavigationStart} from '@angular/router';
1011} )
1112export class MaterialDocsApp {
1213 showShadow = false ;
14+ baseTitle = 'Angular Material' ;
1315
14- constructor ( router : Router ) {
16+ constructor ( router : Router , private _titleService : Title ) {
1517 let previousRoute = router . routerState . snapshot . url ;
1618
1719 router . events . subscribe ( ( data : NavigationStart ) => {
@@ -24,8 +26,33 @@ export class MaterialDocsApp {
2426 }
2527
2628 previousRoute = data . url ;
29+
30+ // set page title
31+ this . _setTitle ( window . location . pathname ) ;
2732 } ) ;
2833 }
34+
35+ private _setTitle ( pathname ) {
36+ const title = this . _getTitle ( pathname ) ;
37+ title ?
38+ this . _titleService . setTitle ( `${ this . baseTitle } - ${ this . _capitalizeTitle ( title ) } ` ) :
39+ this . _titleService . setTitle ( this . baseTitle ) ;
40+ }
41+
42+ private _getTitle ( pathname ) {
43+ return pathname . split ( '/' ) . filter ( Boolean ) . pop ( ) ;
44+ }
45+
46+ private _trimFilename ( filename ) {
47+ const isFilenameRegex = new RegExp ( / .+ \. / g) ;
48+ return ~ filename . search ( isFilenameRegex ) ?
49+ filename . match ( isFilenameRegex ) [ 0 ] . replace ( '.' , '' ) :
50+ filename ;
51+ }
52+
53+ private _capitalizeTitle ( title ) {
54+ return title [ 0 ] . toUpperCase ( ) + this . _trimFilename ( title . slice ( 1 ) ) ;
55+ }
2956}
3057
3158function isNavigationWithinComponentView ( oldUrl : string , newUrl : string ) {
0 commit comments