How to change page title with routing in Angular application?

JS dev picture JS dev · Dec 20, 2017 · Viewed 35.8k times · Source

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.

Answer

John picture John · Dec 20, 2017

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://v2.angular.io/docs/ts/latest/cookbook/set-document-title.html