Created
July 28, 2017 04:31
-
-
Save ragingprodigy/74e4804c339f2121f23ecf4dbbb7e744 to your computer and use it in GitHub Desktop.
Setting dynamic page titles in Angular 2+
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
import { Component, OnInit } from '@angular/core'; | |
import { Title } from '@angular/platform-browser'; | |
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router'; | |
import 'rxjs/add/operator/filter'; | |
import 'rxjs/add/operator/map'; | |
import 'rxjs/add/operator/mergeMap'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements OnInit { | |
constructor( | |
private titleService: Title, | |
private activatedRoute: ActivatedRoute, | |
private router: Router | |
) {} | |
ngOnInit() { | |
this.router.events | |
.filter((event) => event instanceof NavigationEnd) | |
.map(() => this.activatedRoute) | |
.map((route) => { | |
while (route.firstChild) { | |
route = route.firstChild; | |
} | |
return route; | |
}) | |
.filter((route) => route.outlet === 'primary') | |
.mergeMap((route) => route.data) | |
.subscribe((event) => this.titleService.setTitle(event['title'])); | |
} | |
} |
I found here some detailed explanations https://g00glen00b.be/page-title-route-change-angular-2/
All explanations about this script: https://toddmotto.com/dynamic-page-titles-angular-2-router-events
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can Please someone explain a little bit the ngOnInit function? Thanks.