Set Page Title In Angular
In this article, we use an example to demonstrate how to set the page title in Angular projects using the title service. We may easily update the application's HTML title thanks to the title service.
Why Change Page Title
Changing the page title is crucial since it enables search engines to understand the page's purpose and properly index it. Users can also benefit from knowing which page they are on.
Since Angular is a single-page application, the page does not reload entirely. At first, the page merely loads once. When you switch between routes, only a portion of the page loads.
The index.html file contains the title of the page, as displayed below. The title of the application is set using the HTML title tag. All other pages or routes receive the same title because it only loads at the beginning.
<html lang="en"> <head> <meta charset="utf-8"></meta> <title>Title Service Example</title> <base href="/"></base> <meta content="width=device-width, initial-scale=1" name="viewport"></meta> <link href="favicon.ico" rel="icon" type="image/x-icon"></link> </head> <body> <app-root></app-root> </body> </html>
Title Service in Angular
How to Use Title Service
Import it to Angular Module
import { BrowserModule, Title } from '@angular/platform-browser';
@NgModule({ declarations: [ ], imports: [ BrowserModule, ], providers: [ Title //Register the Service ], bootstrap: [AppComponent] }) export class AppModule { }
Inject Title service in the component
export class TitleComponent implements OnInit { constructor(private title:Title) { } }
Use the setTitle method to set the title
ngOnInit() { this.title.setTitle("How to use title service in Angular") }
Title service example
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.component'; import { TwoComponent } from './two-component'; import { ThreeComponent } from './three.component'; @NgModule({ declarations: [ AppComponent,OneComponent,TwoComponent,ThreeComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [Title], bootstrap: [AppComponent] }) export class AppModule { }
Add the following code under the app-routing.module.ts:
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { OneComponent } from './one.component'; import { TwoComponent } from './two-component'; import { ThreeComponent } from './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 { }
Add the following code under the 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); } }
Add the following code under the app.component.html:
<h1>Angular Title Service Example</h1> <ul> <li><a one="" routerlink="">One</a> </li> <li><a routerlink="" two="">two</a> </li> <li><a routerlink="" three="">three</a> </li> </ul> <router-outlet></router-outlet>
Add the following code under the one.component.ts:
@Component({ template: `<h1>One Component</h1>` }) export class OneComponent implements OnInit { title = 'One Component Title'; constructor(private titleService:Title){ } ngOnInit() { this.titleService.setTitle(this.title); } }
Add the following code under the two.component.ts:
import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser'; @Component({ template: `<h1>Two Component</h1>` }) export class TwoComponent implements OnInit { title = 'Two Component Title'; constructor(private titleService:Title) { } ngOnInit() { this.titleService.setTitle(this.title); } }
Add the following code under the three.component.ts:
import { Component, OnInit } from '@angular/core'; import { Title } from '@angular/platform-browser'; @Component({ template: `<h1>Three Component</h1>` }) export class ThreeComponent implements OnInit { title = 'Three Component Title'; constructor(private titleService:Title){} ngOnInit() { this.titleService.setTitle(this.title); } }