| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | {@a top} | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  | # Set the Document Title
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | Your app should be able to make the browser title bar say whatever you want it to say. | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | This cookbook explains how to do it. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-21 17:21:45 -07:00
										 |  |  | See the <live-example name="set-document-title"></live-example>. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## The problem with *<title>*
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The obvious approach is to bind a property of the component to the HTML `<title>` like this: | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | <code-example format=''> | 
					
						
							|  |  |  |   <title>{{This_Does_Not_Work}}</title> | 
					
						
							|  |  |  | </code-example> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sorry but that won't work. | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | The root component of the application is an element contained within the `<body>` tag. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The HTML `<title>` is in the document `<head>`, outside the body, making it inaccessible to Angular data binding. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:00:08 -07:00
										 |  |  | <div class="alert is-helpful"> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  |   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. | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-10 16:51:13 +01:00
										 |  |  | </div> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  | ## Use the `Title` service
 | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | Fortunately, Angular bridges the gap by providing a `Title` service as part of the *Browser platform*. | 
					
						
							| 
									
										
										
										
											2017-04-30 22:10:32 +02:00
										 |  |  | The [Title](api/platform-browser/Title) service is a simple class that provides an API | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | for getting and setting the current HTML document title: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | * `getTitle() : string`—Gets the title of the current HTML document. | 
					
						
							| 
									
										
										
										
											2017-04-26 15:11:02 +03:00
										 |  |  | * `setTitle( newTitle : string )`—Sets the title of the current HTML document. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | You can inject the `Title` service into the root `AppComponent` and expose a bindable `setTitle` method that calls it: | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  | <code-example path="set-document-title/src/app/app.component.ts" region="class" title="src/app/app.component.ts (class)" linenums="false"></code-example> | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | Bind that method to three anchor tags and voilà! | 
					
						
							| 
									
										
										
										
											2017-03-30 20:04:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-09 23:53:32 +01:00
										 |  |  | <figure> | 
					
						
							|  |  |  |   <img src="generated/images/guide/set-document-title/set-title-anim.gif" alt="Set title"> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | </figure> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | Here's the complete solution: | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | <code-tabs> | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  |   <code-pane title="src/main.ts" path="set-document-title/src/main.ts"></code-pane> | 
					
						
							|  |  |  |   <code-pane title="src/app/app.module.ts" path="set-document-title/src/app/app.module.ts"></code-pane> | 
					
						
							|  |  |  |   <code-pane title="src/app/app.component.ts" path="set-document-title/src/app/app.component.ts"></code-pane> | 
					
						
							| 
									
										
										
										
											2017-03-27 16:08:53 +01:00
										 |  |  | </code-tabs> | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 21:31:27 +01:00
										 |  |  | ## Why provide the `Title` service in `bootstrap`
 | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | Generally you want to provide application-wide services in the root application component, `AppComponent`. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | This cookbook recommends registering the title service during bootstrapping, | 
					
						
							|  |  |  | a location you reserve for configuring the runtime Angular environment. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | That's exactly what you're doing. | 
					
						
							| 
									
										
										
										
											2017-02-22 18:09:39 +00:00
										 |  |  | The `Title` service is part of the Angular *browser platform*. | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | If you bootstrap your application into a different platform, | 
					
						
							| 
									
										
										
										
											2017-04-26 15:11:02 +03:00
										 |  |  | you'll have to provide a different `Title` service that understands | 
					
						
							| 
									
										
										
										
											2017-04-12 21:53:18 +02:00
										 |  |  | the concept of a "document title" for that specific platform. | 
					
						
							|  |  |  | Ideally, the application itself neither knows nor cares about the runtime environment. | 
					
						
							| 
									
										
										
										
											2017-04-01 01:57:13 +02:00
										 |  |  | 
 |