1
1
import { Component , ViewEncapsulation } from '@angular/core' ;
2
2
import { Router , NavigationStart } from '@angular/router' ;
3
+ import { Title } from '@angular/platform-browser' ;
3
4
4
5
5
6
@Component ( {
@@ -10,8 +11,9 @@ import {Router, NavigationStart} from '@angular/router';
10
11
} )
11
12
export class MaterialDocsApp {
12
13
showShadow = false ;
14
+ baseTitle = 'Angular Material' ;
13
15
14
- constructor ( router : Router ) {
16
+ constructor ( router : Router , private _titleService : Title ) {
15
17
let previousRoute = router . routerState . snapshot . url ;
16
18
17
19
router . events . subscribe ( ( data : NavigationStart ) => {
@@ -24,8 +26,33 @@ export class MaterialDocsApp {
24
26
}
25
27
26
28
previousRoute = data . url ;
29
+
30
+ // set page title
31
+ this . _setTitle ( window . location . pathname ) ;
27
32
} ) ;
28
33
}
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
+ }
29
56
}
30
57
31
58
function isNavigationWithinComponentView ( oldUrl : string , newUrl : string ) {
0 commit comments