@@ -34,7 +34,7 @@ p Run the #[+liveExampleLink2()].
34
34
35
35
Our `Car` needs an engine and tires. Instead of asking for them,
36
36
the `Car` constructor instantiates its own copies from
37
- the very specific classes, `Engine` and `Tires`.
37
+ the very specific classes `Engine` and `Tires`.
38
38
39
39
What if the `Engine` class evolves and its constructor requires a parameter?
40
40
Our `Car` is broken and stays broken until we rewrite it along the lines of
@@ -422,9 +422,9 @@ block injectable-not-always-needed-in-ts
422
422
423
423
We *can* add it if we really want to. It isn't necessary because the
424
424
`HeroesComponent` is already marked with `@Component`, and this
425
- !{_decorator} class (like `@Directive` and `@Pipe` which we will study later
426
- on) is a subtype of <a href="#{injMetaUrl}">InjectableMetadata</a>. It is in
427
- fact <a href="#{injMetaUrl}"> InjectableMetadata</a> #{_decorator}s that
425
+ !{_decorator} class (like `@Directive` and `@Pipe`, which we'll learn about later)
426
+ is a subtype of <a href="#{injMetaUrl}">InjectableMetadata</a>. It is in
427
+ fact ` InjectableMetadata` #{_decorator}s that
428
428
identify a class as a target for instantiation by an injector.
429
429
430
430
block ts-any-decorator-will-do
@@ -701,7 +701,7 @@ block dart-diff-const-metadata-ctor
701
701
+ makeTabs(
702
702
` dependency-injection/ts/app/heroes/heroes.component.ts,
703
703
dependency-injection/ts/app/heroes/heroes.component.1.ts` ,
704
- null ,
704
+ ',full' ,
705
705
` app/heroes/heroes.component (v3),
706
706
app/heroes/heroes.component (v2)` ,
707
707
stylePattern)
@@ -779,7 +779,8 @@ block what-should-we-use-as-token
779
779
:marked
780
780
### OpaqueToken
781
781
782
- The solution is to define and use an !{opaquetoken}.
782
+ One solution to choosing a provider token for non-class dependencies is
783
+ to define and use an !{opaquetoken}.
783
784
The definition looks like this:
784
785
785
786
+ makeExample('dependency-injection/ts/app/app.config.ts' ,'token' )( format ='.' )
@@ -798,8 +799,8 @@ block what-should-we-use-as-token
798
799
- var configType = _docsFor == ' dart' ? ' <code>Map</code>' : ' <code>AppConfig</code>'
799
800
.l-sub-section
800
801
:marked
801
- Although it plays no role in dependency injection,
802
- the !{configType} interface supports typing of the configuration object within the class.
802
+ Although the !{configType} interface plays no role in dependency injection,
803
+ it supports typing of the configuration object within the class.
803
804
804
805
block dart-map-alternative
805
806
:marked
@@ -813,16 +814,17 @@ block dart-map-alternative
813
814
814
815
Our `HeroService` *requires* a `Logger`, but what if it could get by without
815
816
a logger?
816
- As is illustrated next, we can indicate that to the dependency injection
817
- framework by marking the constructor's argument as optional.
817
+ We can tell Angular that the dependency is optional by annotating the
818
+ constructor argument with `@Optional()`:
818
819
819
820
+ ifDocsFor('ts' )
820
821
+ makeExample('dependency-injection/ts/app/providers.component.ts' ,'import-optional' , '' )
821
822
+ makeExample('dependency-injection/ts/app/providers.component.ts' ,'provider-10-ctor' , '' )( format ='.' )
822
823
823
824
:marked
824
- Be prepared for a null logger. If we don't register one somewhere up the line,
825
- the injector will inject `null`.
825
+ When using `@Optional()`, our code must be prepared for a null value. If we
826
+ don't register a logger somewhere up the line, the injector will set the
827
+ value of `logger` to null.
826
828
827
829
.l-main-section
828
830
:marked
0 commit comments