@@ -56,10 +56,11 @@ figure.image-display
56
56
:marked
57
57
You can cap the bubbling. An intermediate component can declare that it is the "host" component.
58
58
The hunt for providers will climb no higher than the injector for that host component.
59
- This a topic for another day.
59
+ This is a topic for another day.
60
60
61
61
:marked
62
62
### Re-providing a service at different levels
63
+
63
64
You can re-register a provider for a particular dependency token at multiple levels of the injector tree.
64
65
You don't *have* to re-register providers. You shouldn't do so unless you have a good reason.
65
66
But you *can*.
@@ -77,7 +78,6 @@ figure.image-display
77
78
78
79
The ability to configure one or more providers at different levels opens up interesting and useful possibilities.
79
80
80
- :marked
81
81
### Scenario: service isolation
82
82
83
83
Architectural reasons may lead you to restrict access to a service to the application domain where it belongs.
@@ -93,9 +93,10 @@ figure.image-display
93
93
94
94
Instead, provide the `VillainsService` in the `providers` metadata of the `VillainsListComponent` like this:
95
95
96
- + makeExample('hierarchical-dependency-injection/ts/src/app/villains-list.component.ts' , 'metadata' ,'src/app/villains-list.component.ts (metadata)' )( format ='.' )
96
+ + makeExcerpt('src/app/villains-list.component.ts (metadata)' )
97
+
97
98
:marked
98
- By providing `VillainsService` in the `VillainsListComponent` metadata — and nowhere else —,
99
+ By providing `VillainsService` in the `VillainsListComponent` metadata and nowhere else,
99
100
the service becomes available only in the `VillainsListComponent` and its sub-component tree.
100
101
It's still a singleton, but it's a singleton that exist solely in the _villain_ domain.
101
102
@@ -120,20 +121,23 @@ figure.image-display
120
121
121
122
figure.image-display
122
123
img( src ="/resources/images/devguide/dependency-injection/hid-heroes-anim.gif" width ="400" alt ="Heroes in action" )
124
+
123
125
:marked
124
- One might suppose that the `TaxReturnComponent ` has logic to manage and restore changes.
126
+ One might suppose that the `HeroTaxReturnComponent ` has logic to manage and restore changes.
125
127
That would be a pretty easy task for a simple hero tax return.
126
128
In the real world, with a rich tax return data model, the change management would be tricky.
127
129
You might delegate that management to a helper service, as this example does.
128
130
129
131
Here is the `HeroTaxReturnService`.
130
132
It caches a single `HeroTaxReturn`, tracks changes to that return, and can save or restore it.
131
133
It also delegates to the application-wide, singleton `HeroService`, which it gets by injection.
132
- + makeExample('hierarchical-dependency-injection/ts/src/app/hero-tax-return.service.ts' , '' , 'src/app/hero-tax-return.service.ts' )
134
+
135
+ + makeExample('src/app/hero-tax-return.service.ts' )
133
136
134
137
:marked
135
138
Here is the `HeroTaxReturnComponent` that makes use of it.
136
- + makeExample('hierarchical-dependency-injection/ts/src/app/hero-tax-return.component.ts' , null , 'src/app/hero-tax-return.component.ts' )
139
+
140
+ + makeExample('src/app/hero-tax-return.component.ts' )
137
141
138
142
:marked
139
143
The _tax-return-to-edit_ arrives via the input property which is implemented with getters and setters.
@@ -148,7 +152,8 @@ figure.image-display
148
152
149
153
Look closely at the metadata for the `HeroTaxReturnComponent`. Notice the `providers` property.
150
154
151
- + makeExample('hierarchical-dependency-injection/ts/src/app/hero-tax-return.component.ts' , 'providers' )
155
+ + makeExcerpt('src/app/hero-tax-return.component.ts' , 'providers' , '' )
156
+
152
157
:marked
153
158
The `HeroTaxReturnComponent` has its own provider of the `HeroTaxReturnService`.
154
159
Recall that every component _instance_ has its own injector.
@@ -194,7 +199,7 @@ figure.image-display
194
199
:marked
195
200
The code for this _cars_ scenario is in the `car.components.ts` and `car.services.ts` files of the sample
196
201
which you can review and download from the <live-example></live-example>
197
- : marked
202
+
198
203
//- ## Advanced Dependency Injection in Angular
199
204
//- Restrict Dependency Lookups
200
205
//- [TODO] (@Host) This has been postponed for now until we come up with a decent use case
0 commit comments