Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change page title with routing in Angular application?

Is there any npm module/ other way like React-Helmet that allows us to change page title as we route through our Angular application?

PS: I am using Angular 5.

like image 984
Peter Avatar asked Dec 20 '17 07:12

Peter


People also ask

How does Angular determine routing change?

Steps to detect route change in Angular application Urls. Import Router, Event, NavigationStart, NavigationEnd, NavigationError from '@angular/router'. And inject router in the constructor. Subscribe to the NavigationStart, NavigationEnd, NavigationError events of the router.

What is Title in Angular?

A service that can be used to get and set the title of a current HTML document. class Title { getTitle(): string setTitle(newTitle: string) }

What is the difference between routing and navigation in Angular?

Angular provides extensive set of navigation feature to accommodate simple scenario to complex scenario. The process of defining navigation element and the corresponding view is called Routing. Angular provides a separate module, RouterModule to set up the navigation in the Angular application.


1 Answers

You have a TitleService in Angular 5. Inject it in your component's constructor, and use the setTitle() method.

import {Title} from "@angular/platform-browser";  ....  constructor(private titleService:Title) {   this.titleService.setTitle("Some title"); } 

Here are the docs from Angular: https://angular.io/guide/set-document-title

like image 141
John Avatar answered Sep 16 '22 11:09

John