@@ -178,11 +178,6 @@ code-example(format="." language="bash").
178
178
defines a private `heroService` property and identifies it as a `HeroService` injection site.
179
179
:marked
180
180
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.
186
181
187
182
.l-sub-section
188
183
:marked
@@ -204,19 +199,7 @@ code-example(format="." language="html").
204
199
<a id =" child-component" ></a >
205
200
.l-sub-section
206
201
: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.
220
203
:marked
221
204
### *getHeroes* in the *AppComponent*
222
205
We've got the service in a `heroService` private variable. Let's use it.
@@ -384,18 +367,3 @@ code-example(format="." language="html").
384
367
385
368
Back in the `AppComponent`, replace `heroService.getHeroes` with `heroService.getHeroesSlowly`
386
369
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