import { Injectable } from '@angular/core'; import { Router, NavigationEnd, PRIMARY_OUTLET } from '@angular/router'; import { Subscription } from 'rxjs'; import { filter, map, flatMap } from 'rxjs/operators'; import { Title } from '@angular/platform-browser'; @Injectable({ providedIn: 'root' }) export class AppTitleService { sub: Subscription; appName: string; constructor(private titleService: Title, private router: Router) { this.sub = this.router.events.pipe( filter(event => event instanceof NavigationEnd), map(_ => this.router.routerState.root), map(route => { while (route.firstChild) { route = route.firstChild; } return route; }), flatMap(route => route.data)) .subscribe(data => { let title = data.title; if (title != undefined && title != null) { title = 'Siding Portal - '+title; } else if (this.appName) { title = this.appName; } if (title) { this.titleService.setTitle(title); } }); } }