Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 33b5829

Browse files
Foxandxsswardbell
authored andcommitted
docs(toh): put a bit of clarification
closes #1352
1 parent 156b6f4 commit 33b5829

File tree

2 files changed

+5
-41
lines changed

2 files changed

+5
-41
lines changed

public/docs/ts/latest/tutorial/toh-pt3.jade

+3-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ code-example(format=".")
171171
:marked
172172
The `AppComponent`’s template should now look like this
173173

174-
+makeExample('toh-3/ts/app/app.component.ts', 'hero-detail-template', 'app.component.ts (Template)')
174+
+makeExample('toh-3/ts/app/app.component.ts', 'hero-detail-template', 'app.component.ts (Template)')(format='.')
175175
:marked
176176
Thanks to the binding, the `HeroDetailComponent` should receive the hero from the `AppComponent` and display that hero's detail beneath the list.
177177
The detail should update every time the user picks a new hero.
@@ -189,8 +189,7 @@ code-example(format=".")
189189

190190
We tell Angular about it by listing it in the metadata `directives` array. Let's add that array property to the bottom of the
191191
`@Component` configuration object, immediately after the `template` and `styles` properties.
192-
+makeExample('toh-3/ts/app/app.component.ts', 'directives')
193-
192+
+makeExample('toh-3/ts/app/app.component.ts', 'directives', 'app/app.component.ts (Directives)')
194193

195194
:marked
196195
### It works!
@@ -215,7 +214,7 @@ code-example(format=".")
215214
.file hero-detail.component.ts
216215
.file main.ts
217216
.file node_modules ...
218-
.file typings ...
217+
.file typings ...
219218
.file index.html
220219
.file package.json
221220
.file tsconfig.json

public/docs/ts/latest/tutorial/toh-pt4.jade

+2-37
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ code-example(format="." language="bash").
167167

168168
### Inject the *HeroService*
169169

170-
Two lines replace the one line of *new*:
170+
Two lines replace the one line that created with *new*:
171171
1. we add a constructor.
172172
1. we add to the component's `providers` metadata
173173

@@ -178,11 +178,6 @@ code-example(format="." language="bash").
178178
defines a private `heroService` property and identifies it as a `HeroService` injection site.
179179
:marked
180180
Now Angular will know to supply an instance of the `HeroService` when it creates a new `AppComponent`.
181-
182-
Angular has to get that instance from somewhere. That's the role of the Angular *Dependency Injector*.
183-
The **Injector** has a **container** of previously created services.
184-
Either it finds and returns a pre-existing `HeroService` from its container or it creates a new instance, adds
185-
it to the container, and returns it to Angular.
186181

187182
.l-sub-section
188183
:marked
@@ -201,22 +196,7 @@ code-example(format="." language="html").
201196
:marked
202197
The `providers` array tells Angular to create a fresh instance of the `HeroService` when it creates a new `AppComponent`.
203198
The `AppComponent` can use that service to get heroes and so can every child component of its component tree.
204-
<a id="child-component"></a>
205-
.l-sub-section
206-
:marked
207-
### Services and the component tree
208-
209-
Recall that the `AppComponent` creates an instance of `HeroDetail` by virtue of the
210-
`<my-hero-detail>` tag at the bottom of its template. That `HeroDetail` is a child of the `AppComponent`.
211-
212-
If the `HeroDetailComponent` needed its parent component's `HeroService`,
213-
it would ask Angular to inject the service into its constructor which would look just like the one for `AppComponent`:
214-
+makeExample('toh-4/ts/app/app.component.1.ts', 'ctor', 'hero-detail.component.ts (constructor)')
215-
:marked
216-
The `HeroDetailComponent` must *not* repeat its parent's `providers` array! Guess [why](#shadow-provider).
217-
218-
The `AppComponent` is the top level component of our application.
219-
There should be only one instance of that component and only one instance of the `HeroService` in our entire app.
199+
a#child-component
220200
:marked
221201
### *getHeroes* in the *AppComponent*
222202
We've got the service in a `heroService` private variable. Let's use it.
@@ -384,18 +364,3 @@ code-example(format="." language="html").
384364

385365
Back in the `AppComponent`, replace `heroService.getHeroes` with `heroService.getHeroesSlowly`
386366
and see how the app behaves.
387-
388-
.l-main-section
389-
<a id="shadow-provider"></a>
390-
:marked
391-
### Appendix: Shadowing the parent's service
392-
393-
We stated [earlier](#child-component) that if we injected the parent `AppComponent` `HeroService`
394-
into the `HeroDetailComponent`, *we must not add a providers array* to the `HeroDetailComponent` metadata.
395-
396-
Why? Because that tells Angular to create a new instance of the `HeroService` at the `HeroDetailComponent` level.
397-
The `HeroDetailComponent` doesn't want its *own* service instance; it wants its *parent's* service instance.
398-
Adding the `providers` array creates a new service instance that shadows the parent instance.
399-
400-
Think carefully about where and when to register a provider.
401-
Understand the scope of that registration. Be careful not to create a new service instance at the wrong level.

0 commit comments

Comments
 (0)