What's the best way to get header text to change based on the page? I'm currently working from a single header that changes color based on router.url (see previous question here) and I want to change the page header text dynamically.
I feel like the logic should also be similar should I also want to add in images based on the pages as well.
At first I thought to iterate through an array of titles, but that ended up just printing all the titles on all the pages. Then I thought to create a function that check would check the link and return the appropriate title, but I'm not sure how to insert it...
app.component.html
<!-- <div class="set-margin page-title"><h1 *ngFor="let pageTitle of pageTitles">{{ pageTitle.title }}</h1></div> -->
<div class="set-margin page-title">
<!-- <h1 *ngFor="let pageTitle of pageTitles">{{ pageTitle.title }}</h1> -->
<h1>{{ getHeaderText() }}</h1>
</div>
app.component.ts
export class AppComponent {
activatedRoute = '';
title: string;
pageTitles = [
{ 'title': 'About'},
{ 'title': 'Resources'},
{ 'title': 'News' },
{ 'title': 'Contact' }
];
activateRoute(activatedRoute: string) {
this.activatedRoute = activatedRoute;
}
getHeaderText() {
if (this.activatedRoute.includes('about')) {
this.title = 'About';
return this.title;
} else if (this.activatedRoute.includes('resources')) {
this.title = 'Resources';
return this.title;
} else if (this.activatedRoute.includes('newsroom')) {
this.title = 'News';
return this.title;
} else {
this.title = 'Contact';
return this.title;
}
}
constructor(private router: Router) {}
}