Skip to content

Instantly share code, notes, and snippets.

@CryceTruly
Last active July 26, 2019 08:07
Show Gist options
  • Save CryceTruly/64e1b3b8cce18334f3aed077817b3bec to your computer and use it in GitHub Desktop.
Save CryceTruly/64e1b3b8cce18334f3aed077817b3bec to your computer and use it in GitHub Desktop.
Setting dynamic page titles in an angular 2+ app.
Step 1 #Provide a title in the route configuration,Usually in the app.module.ts
{ path: '', component: HomeComponent, data: { title: 'welcome' } },
Step 2 #import {ActivatedRoute } from '@angular/router';
Step 3 #Inject the ActivatedRoute interface in the constructor of your component:
constructor(private router:ActivatedRoute) { }
Step 4,#Subscribe to the data observable and pull out your data variables,use them for what you want.
ngOnInit() {
this.router.data.subscribe(data=> {
# This does the job:
document.title=data['title'];
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment