Introduction
In this article, I will show the steps to set the page title using the title service in Angular apps using an example. The title service allows us the change the HTML title of the application.
Note. Before going through this session, please visit my previous article related to Angular applications as mentioned below.
Step 1. Create 3 Components using the below command
ng g c one
ng g c two
ng g c three
Each component uses the setTitle method of the Title service to set the title of each component.
Step 2. Add code in app.module.ts
import { BrowserModule, Title } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { OneComponent } from './one/one.component';
import { TwoComponent } from './two/two.component';
import { ThreeComponent } from './three/three.component';
@NgModule({
declarations: [
AppComponent,OneComponent,TwoComponent,ThreeComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [Title],
bootstrap: [AppComponent]
})
export class AppModule { }
Step 3. Add code in app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OneComponent } from './one/one.component';
import { TwoComponent } from './two/two.component';
import { ThreeComponent } from './three/three.component';
const routes: Routes = [
{path: 'one', component:OneComponent},
{path: 'two', component:TwoComponent},
{path: 'three', component:ThreeComponent},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Step 4. Add code in app.component.ts
import { Component } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'title Service Example';
constructor(private titleService:Title) {
}
ngOnInit() {
this.titleService.setTitle(this.title);
}
}
Step 5. Add code in app.component.html
<div class="container">
<nav class="navbar navbar-default">
<ul class="nav navbar-nav">
<li>
<a routerLinkActive="active" [routerLink]="['/one']">Male</a>
</li>
<li>
<a routerLinkActive="active" [routerLink]="['/two']">Female</a>
</li>
<li>
<a routerLinkActive="active" [routerLink]="['/three']">Unknown</a>
</li>
</ul>
</nav>
<router-outlet></router-outlet>
</div>
Step 6. Add code in one.component.ts
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
template: `<h1>This is Male.</h1>`
})
export class OneComponent implements OnInit {
title = 'Male - One Component Title';
constructor(private titleService:Title){
}
ngOnInit() {
this.titleService.setTitle(this.title);
}
}
Step 7. Add code in two.component.ts
import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
@Component({
template: `<h1>This is Female.</h1>`
})
export class TwoComponent implements OnInit {
title = 'Female - Two Component Title';
constructor(private titleService:Title) {
}
ngOnInit() {
this.titleService.setTitle(this.title);
}
}




Join the conversation! Your thoughts help the community grow.