@@ -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