Set the Document Title

Set the Document Title

Our app should be able to make the browser title bar say whatever we want it to say. This cookbook explains how to do it.

See the .

To see the browser title bar change in the live example, open it again in the Plunker editor by clicking the icon in the upper right, then pop out the preview window by clicking the blue 'X' button in the upper right corner.

pop out the window
pop out the window

The problem with <title>

The obvious approach is to bind a property of the component to the HTML <title> like this:

<title>{{This_Does_Not_Work}}</title>

Sorry but that won't work. The root component of our application is an element contained within the <body> tag. The HTML <title> is in the document <head>, outside the body, making it inaccessible to Angular data binding.

We could grab the browser document object and set the title manually. That's dirty and undermines our chances of running the app outside of a browser someday.

Running your app outside a browser means that you can take advantage of server-side pre-rendering for near-instant first app render times and for SEO. It means you could run from inside a Web Worker to improve your app's responsiveness by using multiple threads. And it means that you could run your app inside Electron.js or Windows Universal to deliver it to the desktop.

Use the Title service

Fortunately, Angular bridges the gap by providing a Title service as part of the Browser platform. The Title service is a simple class that provides an API for getting and setting the current HTML document title:

  • getTitle() : string — Gets the title of the current HTML document.
  • setTitle( newTitle : string ) — Sets the title of the current HTML document.

Let's inject the Title service into the root AppComponent and expose a bindable setTitle method that calls it:

src/app/app.component.ts (class)

export class AppComponent {
  public constructor(private titleService: Title ) { }

  public setTitle( newTitle: string) {
    this.titleService.setTitle( newTitle );
  }
}

We bind that method to three anchor tags and, voilà!

Set title

Here's the complete solution

src/main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';

platformBrowserDynamic().bootstrapModule(AppModule);
src/app/app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule, Title }  from '@angular/platform-browser';

import { AppComponent } from './app.component';

@NgModule({
  imports: [
    BrowserModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [
    Title
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }
src/app/app.component.ts
// Import the native Angular services.
import { Component } from '@angular/core';
import { Title }     from '@angular/platform-browser';

@Component({
selector: 'my-app',
template:
  `<p>
    Select a title to set on the current HTML document:
  </p>

  <ul>
    <li><a (click)="setTitle( 'Good morning!' )">Good morning</a>.</li>
    <li><a (click)="setTitle( 'Good afternoon!' )">Good afternoon</a>.</li>
    <li><a (click)="setTitle( 'Good evening!' )">Good evening</a>.</li>
  </ul>
  `
})
export class AppComponent {
  public constructor(private titleService: Title ) { }

  public setTitle( newTitle: string) {
    this.titleService.setTitle( newTitle );
  }
}

Why we provide the Title service in bootstrap

We generally recommended providing application-wide services in the root application component, AppComponent.

Here we recommend registering the title service during bootstrapping, a location we reserve for configuring the runtime Angular environment.

That's exactly what we're doing. The Title service is part of the Angular browser platform. If we bootstrap our application into a different platform, we'll have to provide a different Title service that understands the concept of a "document title" for that specific platform. Ideally the application itself neither knows nor cares about the runtime environment.

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/docs/ts/latest/cookbook/set-document-title.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部