All files / app/core app-title-strategy.service.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                  1x           2x 2x   2x               2x 2x 1x   1x        
import { inject, Injectable } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { TitleStrategy } from '@angular/router';
import type { RouterStateSnapshot } from '@angular/router';
 
/**
 * Sets the current HTML page title using the title value from the route.
 */
@Injectable({ providedIn: 'root' })
export class AppTitleStrategyService extends TitleStrategy {
  /** Title tag content from index.html */
  private readonly _baseTitle: string;
  private readonly _title: Title;
 
  constructor() {
    super();
    this._title = inject(Title);
    // Tested in `ng serve` and `ng build` that this gets the <title> from index.html
    this._baseTitle = this._title.getTitle();
  }
 
  /**
   * Sets the current title for the page from the route, suffixing with the value in the title
   * element in index.html.
   */
  public updateTitle(snapshot: RouterStateSnapshot): void {
    const pageTitle = this.buildTitle(snapshot);
    if (pageTitle == undefined) {
      this._title.setTitle(this._baseTitle);
    } else {
      this._title.setTitle(`${pageTitle} | ${this._baseTitle}`);
    }
  }
}