I want to change the title of the page whenever I click or browse the link from the URL bar. How to change that using Angular route? I am using angular version 4 and angular cli.
-
*How to change that using Angular route?* By searching for "change page title angular2". – May 16 '17 at 11:29
-
1Possible duplicate of [how to change page title in angular2 router](http://stackoverflow.com/questions/34602806/how-to-change-page-title-in-angular2-router) – May 16 '17 at 11:30
-
Thanks, but that code was not working, or else I was not able to implement that in my code. – Shakti May 17 '17 at 07:27
-
Then present what you did, and in what particular way it was not working, and/or in what particular way you were not able to implement it in your code. – May 17 '17 at 08:29
4 Answers
You can to use @angular/plateform-browser
to use the setTitle()
:
import { Title } from '@angular/platform-browser';
@Component({
selector: 'your-component',
})
export class YourComponent implements onInit {
constructor(private title: Title) {}
ngOnInit() {
this.title.setTitle('Title for this component');
}
}

- 100,159
- 46
- 371
- 480

- 8,783
- 6
- 58
- 79
-
-
2It might be not related to this question, is there a way to change it back after redirecting to other pages other than `setTitle` in all other pages? Since this change will be kept after leaving that page. – Jason Liu Sep 19 '18 at 16:56
you have to pass "title" as data to your route
const routes: Routes = [{
path: 'calendar',
component: CalendarComponent,
children: [
{ path: '', redirectTo: 'new', pathMatch: 'full' },
{ path: 'all', component: CalendarListComponent, data: { title: 'My Calendar' } },
{ path: 'new', component: CalendarEventComponent, data: { title: 'New Calendar Entry' } },
{ path: ':id', component: CalendarEventComponent, data: { title: 'Calendar Entry' } }
]
}];
Then do this imports in your Component:
import { Title } from '@angular/platform-browser';
import { Router, NavigationEnd, ActivatedRoute } from '@angular/router';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
Once imported, we can inject them both:
@Component({
selector: 'app-root',
templateUrl: `
<div>
Hello world!
</div>
`
})
export class AppComponent {
constructor( private router: Router,
private activatedRoute: ActivatedRoute,
private titleService: Title) {}
}
To update a page title statically, we can simply call setTitle like so:
ngOnInit() {
this.router.events
.filter((event) => event instanceof NavigationEnd)
.map(() => this.activatedRoute)
.map((route) => {
while (route.firstChild) route = route.firstChild;
return route;
})
.filter((route) => route.outlet === 'primary')
.mergeMap((route) => route.data)
.subscribe((event) => {
let title = 'Default Title Here'
if(event['title']) {
title = event['title'];
}
this.titleService.setTitle(title);
});
}

- 3,061
- 5
- 19
- 36

- 403
- 2
- 5
-
Thanks for the answer. But I want to know that, is this the complete code for changing the title? I implemented this but still I was not able to change the title. As I am a beginner in angular, can you send me the complete code? – Shakti May 18 '17 at 11:10
-
8see the following for the full code: https://toddmotto.com/dynamic-page-titles-angular-2-router-events – J King May 19 '17 at 14:46
-
11-1: The `data: { title: '...'}` is not used in this example and so it does not answer the question directly. Seems like a fast copy paste from the link @JKing provided – Enes Sadık Özbek Dec 07 '17 at 05:55
-
1-1 also. At least give credit where you copy pasted this answer. https://toddmotto.com/dynamic-page-titles-angular-2-router-events – Edgar Quintero Feb 20 '19 at 20:17
I have tried with following way with few lines of code, hope it will help you.
You can pass any name in the data object of routes variable (app-routing.module.ts) as follows
const routes: Routes = [
{
path: '',
component: Component,
data: {
page_title: 'Title of your page'
}
}
]
Then fetch the value of page_title in the data object of the routes into the app.component.ts and set as title.
import { Router, RoutesRecognized } from '@angular/router';
import { Title } from '@angular/platform-browser';
export class AppComponent {
page_title = '';
constructor(private titleService: Title, private route: Router) {
route.events.subscribe(event => {
if (event instanceof RoutesRecognized) {
let current_route = event.state.root.firstChild;
this.page_title = current_route?.data.page_title;
if (this.page_title == undefined) {
alert('Please set page title for the routing')
}
else {
titleService.setTitle(this.page_title);
}
}
});
}
}
You can also try to declare the service then copy the codes in the constructor to a function inside the service and do the necessary imports, After call the function of service inside constructor of app.component.ts which will help in future projects.

- 1,111
- 1
- 9
- 14
Angular version 14 (2022)
Just add a title key in the routes const. For instance
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
// Components
import {ViewHomeComponent} from "./components/home.component";
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full'},
{ path: 'home', component: ViewHomeComponent, title: 'Home' }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export default class ModuleHomeRouting {}
That will set automatically the title to 'Home'!

- 6,013
- 5
- 30
- 38