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.
<title>的问题
The problem with <title>
显而易见的方法是把组件的属性绑定到HTML的<title>标签上,像这样:
The obvious approach is to bind a property of the component to the HTML <title> like this:
Sorry but that won't work.
The root component of the 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.
You could grab the browser document object and set the title manually.
That's dirty and undermines your 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.
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 —— 获取当前HTML文档的标题。
getTitle() : string—Gets the title of the current HTML document.
setTitle( newTitle: string) —— 设置当前HTML文档的标题。
setTitle( newTitle : string )—Sets the title of the current HTML document.
That's exactly what you're doing.
The Title service is part of the Angular browser platform.
If you bootstrap your application into a different platform,
you'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.