Skip to content

Commit

Permalink
feat(title): add route title data support and display title when found
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed Oct 19, 2017
1 parent 579e753 commit 7ab617f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 8 deletions.
Empty file modified CHANGELOG.md
100755 → 100644
Empty file.
Empty file modified package.json
100755 → 100644
Empty file.
5 changes: 4 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ const routes: Routes = [
},
{
path: 'settings',
component: SettingsComponent
component: SettingsComponent,
data: {
title: 'Settings'
}
},
{
path: 'examples',
Expand Down
20 changes: 18 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, HostBinding, OnDestroy, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, HostBinding, OnDestroy, OnInit } from '@angular/core';
import { ActivationEnd, Router } from '@angular/router';
import { Store } from '@ngrx/store';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';
Expand Down Expand Up @@ -40,7 +42,9 @@ export class AppComponent implements OnInit, OnDestroy {

constructor(
public overlayContainer: OverlayContainer,
private store: Store<any>
private store: Store<any>,
private router: Router,
private titleService: Title
) {}

ngOnInit(): void {
Expand All @@ -56,6 +60,18 @@ export class AppComponent implements OnInit, OnDestroy {
.select(selectorAuth)
.takeUntil(this.unsubscribe$)
.subscribe(auth => (this.isAuthenticated = auth.isAuthenticated));
this.router.events
.filter(event => event instanceof ActivationEnd)
.subscribe((event: ActivationEnd) => {
let lastChild = event.snapshot;
while (lastChild.children.length) {
lastChild = lastChild.children[0];
}
const { title } = lastChild.data;
this.titleService.setTitle(
title ? `${title} - ${env.appName}` : env.appName
);
});
}

ngOnDestroy(): void {
Expand Down
15 changes: 12 additions & 3 deletions src/app/examples/examples-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,24 @@ const routes: Routes = [
},
{
path: 'todos',
component: TodosComponent
component: TodosComponent,
data: {
title: 'Todos'
}
},
{
path: 'stock-market',
component: StockMarketComponent
component: StockMarketComponent,
data: {
title: 'Stock Market'
}
},
{
path: 'theming',
component: ParentComponent
component: ParentComponent,
data: {
title: 'Theming'
}
}
]
}
Expand Down
8 changes: 6 additions & 2 deletions src/app/static/static-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import { AboutComponent } from './about/about.component';
import { FeaturesComponent } from './features/features.component';

const routes: Routes = [
{ path: 'about', component: AboutComponent },
{ path: 'features', component: FeaturesComponent }
{ path: 'about', component: AboutComponent, data: { title: 'About' } },
{
path: 'features',
component: FeaturesComponent,
data: { title: 'Features' }
}
];

@NgModule({
Expand Down
1 change: 1 addition & 0 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const packageJson = require('../../package.json');

export const environment = {
appName: 'Angular Ngrx Material Starter',
envName: 'PROD',
production: true,
versions: {
Expand Down
1 change: 1 addition & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const packageJson = require('../../package.json');

export const environment = {
appName: 'Angular Ngrx Material Starter',
envName: 'DEV',
production: false,
versions: {
Expand Down

0 comments on commit 7ab617f

Please sign in to comment.