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

Commit 3223fec

Browse files
committed
docs(toh): put a bit of clarification
1 parent 12984fd commit 3223fec

File tree

2 files changed

+3
-36
lines changed

2 files changed

+3
-36
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -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')
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

+1-33
Original file line numberDiff line numberDiff line change
@@ -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
@@ -204,19 +199,7 @@ code-example(format="." language="html").
204199
<a id="child-component"></a>
205200
.l-sub-section
206201
: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.
202+
Learn more about hierarchies in the [Hierarchical Dependency Injection](../guide/hierarchical-dependency-injection.html) chapter.
220203
:marked
221204
### *getHeroes* in the *AppComponent*
222205
We've got the service in a `heroService` private variable. Let's use it.
@@ -384,18 +367,3 @@ code-example(format="." language="html").
384367

385368
Back in the `AppComponent`, replace `heroService.getHeroes` with `heroService.getHeroesSlowly`
386369
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)