@@ -9,21 +9,21 @@ a(id='top')
9
9
.l-sub-section
10
10
img( src ='/resources/images/devguide/plunker-separate-window-button.png' alt ="pop out the window" align ="right" style ="margin-right:-20px" )
11
11
:marked
12
- To see the browser Title bar changes,
12
+ To see the browser Title bar changes,
13
13
pop out the preview window by clicking the blue 'X' button in the upper right corner.
14
14
:marked
15
15
## The problem with *<title>*
16
-
16
+
17
17
The obvious approach is to bind a property of the component to the HTML `<title>` like this:
18
- code-example( format ='' )
18
+ code-example( format ='' ) .
19
19
< title> {{This_Does_Not_Work}}< /title>
20
20
:marked
21
- Sorry but that won't work.
21
+ Sorry but that won't work.
22
22
The root component of our application is an element contained within the `<body>` tag.
23
23
The HTML `<title>` is in the document `<head>`, outside the body, making it inaccessible to Angular data binding.
24
24
25
- We could grab the browser `document` object and set the title manually.
26
- That's dirty and undermines our chances of running the app outside of a browser someday.
25
+ We could grab the browser `document` object and set the title manually.
26
+ That's dirty and undermines our chances of running the app outside of a browser someday.
27
27
.l-sub-section
28
28
:marked
29
29
Running your app outside a browser means that you can take advantage of server-side
@@ -40,16 +40,16 @@ code-example(format='')
40
40
* `getTitle() : string` — Gets the title of the current HTML document.
41
41
* `setTitle( newTitle : string )` — Sets the title of the current HTML document.
42
42
43
- While this class is part of the Browser platform package, it is *not part of the default Browser
44
- platform providers* that Angular loads automatically.
43
+ While this class is part of the Browser platform package, it is *not part of the default Browser
44
+ platform providers* that Angular loads automatically.
45
45
This means as we bootstrap our application using the Browser platform `boostrap()`
46
46
function, we'll also have to include `Title` service explicitly as one of the bootstrap providers:
47
47
48
48
+ makeExample( "cb-set-document-title/ts/app/main.ts" , "bootstrap-title" , "app/main.ts (provide Title service)" )( format ='.' )
49
49
:marked
50
50
Once we've explicitly provided the `Title` service we can then inject the `Title` service into any of our
51
- custom application components and services.
52
-
51
+ custom application components and services.
52
+
53
53
Let's inject the `Title` service into the root `AppComponent` and expose a bindable `setTitle` method that calls it:
54
54
55
55
+ makeExample( "cb-set-document-title/ts/app/app.component.ts" , "class" , "app/app.component.ts (class)" )( format ='.' )
@@ -61,31 +61,31 @@ figure.image-display
61
61
:marked
62
62
Here's the complete solution
63
63
64
- + makeTabs(
64
+ + makeTabs(
65
65
` cb-set-document-title/ts/app/main.ts,
66
- cb-set-document-title/ts/app/app.component.ts` ,
67
- '' ,
66
+ cb-set-document-title/ts/app/app.component.ts` ,
67
+ '' ,
68
68
'app/main.ts, app/app.component.ts' )
69
69
70
70
//
71
- Todo: tie this back to the router so we can see how to use this Title service to (re)set the title
71
+ Todo: tie this back to the router so we can see how to use this Title service to (re)set the title
72
72
that appears in the window navigation history and shows up in the back/forward buttons
73
73
during routing.
74
-
74
+
75
75
See https://github.com/angular/angular/issues/7630#issuecomment-198328802
76
-
76
+
77
77
.l-main-section
78
78
:marked
79
79
## Why we provide the *Title* service in *bootstrap*
80
-
80
+
81
81
We generally recommended providing application-wide services in the root application component, `AppComponent`.
82
-
83
- Here we recommend registering the title service during bootstrapping,
82
+
83
+ Here we recommend registering the title service during bootstrapping,
84
84
a location we reserve for configuring the runtime Angular environment.
85
-
85
+
86
86
That's exactly what we're doing.
87
- The `Title` service is part of the Angular *browser platform*.
88
- If we bootstrap our application into a different platform,
87
+ The `Title` service is part of the Angular *browser platform*.
88
+ If we bootstrap our application into a different platform,
89
89
we'll have to provide a different `Title` service that understands the concept of a "document title" for that specific platform.
90
90
Ideally the application itself neither knows nor cares about the runtime environment.
91
91
:marked
0 commit comments