feat(aio): set the document title in the browser title

This commit is contained in:
Peter Bacon Darwin 2017-04-16 22:11:00 +01:00 committed by Tobias Bosch
parent 5293794316
commit 69b86925b3
2 changed files with 19 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import { async, inject, ComponentFixture, TestBed } from '@angular/core/testing';
import { Title } from '@angular/platform-browser';
import { APP_BASE_HREF } from '@angular/common';
import { Http } from '@angular/http';
import { By } from '@angular/platform-browser';
@ -264,6 +265,13 @@ describe('AppComponent', () => {
expect(docViewer.innerText).toMatch(/Test Doc/i);
});
it('should update the document title', () => {
const titleService = TestBed.get(Title);
spyOn(titleService, 'setTitle');
locationService.go('guide/pipes');
fixture.detectChanges();
expect(titleService.setTitle).toHaveBeenCalledWith('Angular - Pipes');
});
});
describe('autoScrolling', () => {

View File

@ -1,6 +1,7 @@
import { Component, ElementRef, HostListener, OnInit,
QueryList, ViewChild, ViewChildren } from '@angular/core';
import { MdSidenav } from '@angular/material';
import { Title } from '@angular/platform-browser';
import { AutoScrollService } from 'app/shared/auto-scroll.service';
import { CurrentNode, NavigationService, NavigationViews, NavigationNode, VersionInfo } from 'app/navigation/navigation.service';
@ -60,13 +61,17 @@ export class AppComponent implements OnInit {
private documentService: DocumentService,
private locationService: LocationService,
private navigationService: NavigationService,
private swUpdateNotifications: SwUpdateNotificationsService
private swUpdateNotifications: SwUpdateNotificationsService,
private titleService: Title
) { }
ngOnInit() {
/* No need to unsubscribe because this root component never dies */
this.documentService.currentDocument.subscribe(doc => this.currentDocument = doc);
this.documentService.currentDocument.subscribe(doc => {
this.currentDocument = doc;
this.setDocumentTitle(doc.title);
});
// scroll even if only the hash fragment changed
this.locationService.currentUrl.subscribe(url => this.autoScroll());
@ -138,4 +143,8 @@ export class AppComponent implements OnInit {
sideNavToggle(value?: boolean) {
this.sidenav.toggle(value);
}
setDocumentTitle(title) {
this.titleService.setTitle(`Angular - ${title}`);
}
}