I would like to change the main title of my application every time I change my route like My first application - Dashboard or My first application - Statistic
Here is my code, I use Material UI :
defaut.component.html:
<div> <mat-toolbar> <button mat-icon-button *ngIf="sidenav.mode === 'over'" (click)="sidenav.toggle()"> <mat-icon *ngIf="!sidenav.opened"> menu </mat-icon> <mat-icon *ngIf="sidenav.opened"> close </mat-icon> </button> <img class="logo" src=".." alt="logo" /> <h1 class="title">My first application - </h1> // Title </mat-toolbar> <mat-sidenav-container> <mat-sidenav #sidenav="matSidenav"> <app-sidebar></app-sidebar> </mat-sidenav> <mat-sidenav-content> <div class="main"> <router-outlet></router-outlet> </div> </mat-sidenav-content> </mat-sidenav-container> </div> defaut.component.ts:
import { AfterViewInit, Component, ViewChild } from '@angular/core'; import { BreakpointObserver } from '@angular/cdk/layout'; import { MatSidenav } from '@angular/material/sidenav'; @Component({ selector: 'app-default', templateUrl: './default.component.html', styleUrls: ['./default.component.css'] }) export class DefaultComponent implements AfterViewInit { @ViewChild(MatSidenav) sidenav!: MatSidenav; constructor(private observer: BreakpointObserver) { } ngAfterViewInit(): void { this.observer.observe(['(max-width:959px)']).subscribe((res) => { if (res.matches) { this.sidenav.mode = 'over'; this.sidenav.close() } else { this.sidenav.mode = 'side'; this.sidenav.open() } }) } } sidebar.component.html:
<div class="navbar"> <h2>Menu</h2> <div class="menu-item"> <button mat-button class="menu-button" routerLink="/app/dashboard" routerLinkActive="active" style="text-decoration:none"> <span>Dashboard</span> </button> <button mat-button class="menu-button" routerLink="/app/statistic" routerLinkActive="active" style="text-decoration:none"> <span>Statistic</span> </button> </div> </div>
<h1 class="title">My first application - </h1>?