From 82b490f44ed4cd5d91978dae12f9bfe5a6d63810 Mon Sep 17 00:00:00 2001 From: Kapunahele Wong Date: Fri, 17 Mar 2017 18:26:57 -0400 Subject: [PATCH] docs(DI): copy edits and TOC update (#3381) --- .../ts/latest/guide/dependency-injection.jade | 482 ++++++++++-------- 1 file changed, 261 insertions(+), 221 deletions(-) diff --git a/public/docs/ts/latest/guide/dependency-injection.jade b/public/docs/ts/latest/guide/dependency-injection.jade index 172b8a70aa..a7abd7a0ec 100644 --- a/public/docs/ts/latest/guide/dependency-injection.jade +++ b/public/docs/ts/latest/guide/dependency-injection.jade @@ -5,17 +5,39 @@ block includes :marked **Dependency injection** is an important application design pattern. Angular has its own dependency injection framework, and - we really can't build an Angular application without it. + you really can't build an Angular application without it. It's used so widely that almost everyone just calls it _DI_. - In this chapter we'll learn what DI is and why we want it. - Then we'll learn [how to use it](#angular-di) in an Angular app. - - - [Why dependency injection?](#why-dependency-injection) + This page covers what DI is, why it's so useful, + and [how to use it](#angular-di) in an Angular app. +:marked + # Contents + - [Why dependency injection?](#why-di) - [Angular dependency injection](#angular-dependency-injection) + - [Configuring the injector](#injector-config) + - [Registering providers in an `NgModule`](#register-providers-ngmodule) + - [Registering providers in a component](#register-providers-component) + - [When to use `NgModule` versus an application component](#ngmodule-vs-comp) + - [Preparing the `HeroListComponent` for injection](#prep-for-injection) + - [Implicit injector creation](#di-metadata) + - [Singleton services](#singleton-services) + - [Testing the component](#testing-the-component) + - [When the service needs a service](#service-needs-service) + - [Why `@Injectable()`?](#injectable) + - [Creating and registering a logger service](#logger-service) - [Injector providers](#injector-providers) + - [The `Provider` class and `provide` object literal](#provide) + - [Alternative class providers](#class-provider) + - [Class provider with dependencies](#class-provider-dependencies) + - [Aliased class providers](#aliased-class-providers) + - [Value providers](#value-provider) + - [Factory providers](#factory-provider) - [Dependency injection tokens](#dependency-injection-tokens) + - [Non-class dependencies](#non-class-dependencies) + - [`OpaqueToken`](#opaquetoken) + - [Optional dependencies](#optional) - [Summary](#summary) + - [Appendix: Working with injectors directly](#explicit-injector) Run the . @@ -23,54 +45,56 @@ block includes :marked ## Why dependency injection? - Let's start with the following code. + To understand why dependency injection is so important, consider an example without it. + Imagine writing the following code: +makeExample('dependency-injection/ts/src/app/car/car-no-di.ts', 'car', 'src/app/car/car.ts (without DI)') :marked - Our `Car` creates everything it needs inside its constructor. + The `Car` class creates everything it needs inside its constructor. What's the problem? - The problem is that our `Car` class is brittle, inflexible, and hard to test. + The problem is that the `Car` class is brittle, inflexible, and hard to test. - Our `Car` needs an engine and tires. Instead of asking for them, + This `Car` needs an engine and tires. Instead of asking for them, the `Car` constructor instantiates its own copies from the very specific classes `Engine` and `Tires`. What if the `Engine` class evolves and its constructor requires a parameter? - Our `Car` is broken and stays broken until we rewrite it along the lines of + That would break the `Car` class and it would stay broken until you rewrote it along the lines of `#{_thisDot}engine = new Engine(theNewParameter)`. - We didn't care about `Engine` constructor parameters when we first wrote `Car`. - We don't really care about them now. - But we'll *have* to start caring because - when the definition of `Engine` changes, our `Car` class must change. + The `Engine` constructor parameters weren't even a consideration when you first wrote `Car`. + You may not anticipate them even now. + But you'll *have* to start caring because + when the definition of `Engine` changes, the `Car` class must change. That makes `Car` brittle. - What if we want to put a different brand of tires on our `Car`? Too bad. - We're locked into whatever brand the `Tires` class creates. That makes our `Car` inflexible. + What if you want to put a different brand of tires on your `Car`? Too bad. + You're locked into whatever brand the `Tires` class creates. That makes the + `Car` class inflexible. - Right now each new car gets its own engine. It can't share an engine with other cars. + Right now each new car gets its own `engine`. It can't share an `engine` with other cars. While that makes sense for an automobile engine, - we can think of other dependencies that should be shared, such as the onboard - wireless connection to the manufacturer's service center. Our `Car` lacks the flexibility + surely you can think of other dependencies that should be shared, such as the onboard + wireless connection to the manufacturer's service center. This `Car` lacks the flexibility to share services that have been created previously for other consumers. - When we write tests for our `Car` we're at the mercy of its hidden dependencies. + When you write tests for `Car` you're at the mercy of its hidden dependencies. Is it even possible to create a new `Engine` in a test environment? - What does `Engine`itself depend upon? What does that dependency depend on? + What does `Engine` depend upon? What does that dependency depend on? Will a new instance of `Engine` make an asynchronous call to the server? - We certainly don't want that going on during our tests. + You certainly don't want that going on during tests. - What if our `Car` should flash a warning signal when tire pressure is low? - How do we confirm that it actually does flash a warning - if we can't swap in low-pressure tires during the test? + What if the `Car` should flash a warning signal when tire pressure is low? + How do you confirm that it actually does flash a warning + if you can't swap in low-pressure tires during the test? - We have no control over the car's hidden dependencies. - When we can't control the dependencies, a class becomes difficult to test. + You have no control over the car's hidden dependencies. + When you can't control the dependencies, a class becomes difficult to test. - How can we make `Car` more robust, flexible, and testable? + How can you make `Car` more robust, flexible, and testable? - That's super easy. We change our `Car` constructor to a version with DI: + That's super easy. Change the `Car` constructor to a version with DI: +makeTabs( 'dependency-injection/ts/src/app/car/car.ts, dependency-injection/ts/src/app/car/car-no-di.ts', @@ -78,29 +102,30 @@ block includes 'src/app/car/car.ts (excerpt with DI), src/app/car/car.ts (excerpt without DI)')(format=".") :marked - See what happened? We moved the definition of the dependencies to the constructor. - Our `Car` class no longer creates an engine or tires. + See what happened? The definition of the dependencies are + now in the constructor. + The `Car` class no longer creates an `engine` or `tires`. It just consumes them. block ctor-syntax .l-sub-section :marked - We also leveraged TypeScript's constructor syntax for declaring + This example leverages TypeScript's constructor syntax for declaring parameters and properties simultaneously. :marked - Now we create a car by passing the engine and tires to the constructor. + Now you can create a car by passing the engine and tires to the constructor. +makeExample('dependency-injection/ts/src/app/car/car-creations.ts', 'car-ctor-instantiation', '')(format=".") :marked How cool is that? - The definition of the engine and tire dependencies are - decoupled from the `Car` class itself. - We can pass in any kind of engine or tires we like, as long as they - conform to the general API requirements of an engine or tires. + The definition of the `engine` and `tire` dependencies are + decoupled from the `Car` class. + You can pass in any kind of `engine` or `tires` you like, as long as they + conform to the general API requirements of an `engine` or `tires`. - If someone extends the `Engine` class, that is not `Car`'s problem. + Now, if someone extends the `Engine` class, that is not `Car`'s problem. .l-sub-section :marked @@ -111,20 +136,20 @@ block ctor-syntax +makeExample('dependency-injection/ts/src/app/car/car-creations.ts', 'car-ctor-instantiation-with-param', '', stylePattern)(format=".") :marked - The critical point is this: `Car` itself did not have to change. - We'll take care of the consumer's problem soon enough. + The critical point is this: the `Car` class did not have to change. + You'll take care of the consumer's problem shortly. :marked - The `Car` class is much easier to test because we are in complete control + The `Car` class is much easier to test now because you are in complete control of its dependencies. - We can pass mocks to the constructor that do exactly what we want them to do + You can pass mocks to the constructor that do exactly what you want them to do during each test: - var stylePattern = { otl: /(new Car.*$)/gm }; +makeExample('dependency-injection/ts/src/app/car/car-creations.ts', 'car-ctor-instantiation-with-mocks', '', stylePattern)(format=".") :marked - **We just learned what dependency injection is**. + **You just learned what dependency injection is**. It's a coding pattern in which a class receives its dependencies from external sources rather than creating them itself. @@ -133,9 +158,9 @@ block ctor-syntax Anyone who wants a `Car` must now create all three parts: the `Car`, `Engine`, and `Tires`. The `Car` class shed its problems at the consumer's expense. - We need something that takes care of assembling these parts for us. + You need something that takes care of assembling these parts. - We could write a giant class to do that: + You _could_ write a giant class to do that: +makeExample('dependency-injection/ts/src/app/car/car-factory.ts', null, 'src/app/car/car-factory.ts') @@ -145,27 +170,27 @@ block ctor-syntax This factory is going to become a huge spiderweb of interdependent factory methods! - Wouldn't it be nice if we could simply list the things we want to build without + Wouldn't it be nice if you could simply list the things you want to build without having to define which dependency gets injected into what? This is where the dependency injection framework comes into play. Imagine the framework had something called an _injector_. - We register some classes with this injector, and it figures out how to create them. + You register some classes with this injector, and it figures out how to create them. - When we need a `Car`, we simply ask the injector to get it for us and we're good to go. + When you need a `Car`, you simply ask the injector to get it for you and you're good to go. +makeExample('dependency-injection/ts/src/app/car/car-injector.ts','injector-call')(format=".") :marked Everyone wins. The `Car` knows nothing about creating an `Engine` or `Tires`. The consumer knows nothing about creating a `Car`. - We don't have a gigantic factory class to maintain. + You don't have a gigantic factory class to maintain. Both `Car` and consumer simply ask for what they need and the injector delivers. This is what a **dependency injection framework** is all about. - Now that we know what dependency injection is and appreciate its benefits, - let's see how it is implemented in Angular. + Now that you know what dependency injection is and appreciate its benefits, + read on to see how it is implemented in Angular. .l-main-section#angular-di :marked @@ -174,11 +199,9 @@ block ctor-syntax Angular ships with its own dependency injection framework. This framework can also be used as a standalone module by other applications and frameworks. - That sounds nice. What does it do for us when building components in Angular? - Let's see, one step at a time. - - We'll begin with a simplified version of the `HeroesComponent` - that we built in the [The Tour of Heroes](../tutorial/). + To see what it can do when building components in Angular, + start with a simplified version of the `HeroesComponent` + that from the [The Tour of Heroes](../tutorial/). +makeTabs( `dependency-injection/ts/src/app/heroes/heroes.component.1.ts, @@ -194,109 +217,121 @@ block ctor-syntax :marked The `HeroesComponent` is the root component of the *Heroes* feature area. It governs all the child components of this area. - Our stripped down version has only one child, `HeroListComponent`, + This stripped down version has only one child, `HeroListComponent`, which displays a list of heroes. :marked Right now `HeroListComponent` gets heroes from `HEROES`, an in-memory collection defined in another file. That may suffice in the early stages of development, but it's far from ideal. - As soon as we try to test this component or want to get our heroes data from a remote server, - we'll have to change the implementation of `heroes` and + As soon as you try to test this component or want to get your heroes data from a remote server, + you'll have to change the implementation of `heroes` and fix every other use of the `HEROES` mock data. - Let's make a service that hides how we get hero data. + It's better to make a service that hides how the app gets hero data. .l-sub-section :marked Given that the service is a [separate concern](https://en.wikipedia.org/wiki/Separation_of_concerns), - we suggest that you - write the service code in its own file. + consider writing the service code in its own file. +ifDocsFor('ts') :marked See [this note](#one-class-per-file) for details. +:marked + The following `HeroService` exposes a `getHeroes` method that returns + the same mock data as before, but none of its consumers need to know that. +makeExample('dependency-injection/ts/src/app/heroes/hero.service.1.ts',null, 'src/app/heroes/hero.service.ts' ) :marked - Our `HeroService` exposes a `getHeroes` method that returns - the same mock data as before, but none of its consumers need to know that. .l-sub-section :marked - Notice the `@Injectable()` #{_decorator} above the service class. - We'll discuss its purpose [shortly](#injectable). + The `@Injectable()` #{_decorator} above the service class is + covered [shortly](#injectable). - var _perhaps = _docsFor == 'dart' ? '' : 'perhaps'; .l-sub-section :marked - We aren't even pretending this is a real service. - If we were actually getting data from a remote server, the API would have to be + Of course, this isn't a real service. + If the app were actually getting data from a remote server, the API would have to be asynchronous, #{_perhaps} returning a !{_PromiseLinked}. - We'd also have to rewrite the way components consume our service. - This is important in general, but not to our current story. + You'd also have to rewrite the way components consume the service. + This is important in general, but not in this example. :marked A service is nothing more than a class in Angular. - It remains nothing more than a class until we register it with an Angular injector. + It remains nothing more than a class until you register it with an Angular injector. #bootstrap +a#injector-config :marked ### Configuring the injector - We don't have to create an Angular injector. - Angular creates an application-wide injector for us during the bootstrap process. + You don't have to create an Angular injector. + Angular creates an application-wide injector for you during the bootstrap process. +makeExcerpt('src/main.ts', 'bootstrap') :marked - We do have to configure the injector by registering the **providers** - that create the services our application requires. - We'll explain what [providers](#providers) are later in this chapter. + You do have to configure the injector by registering the **providers** + that create the services the application requires. + This guide explains what [providers](#providers) are later. block register-provider-ngmodule :marked - We can either register a provider within an [NgModule](ngmodule.html) or in application components + You can either register a provider within an [NgModule](ngmodule.html) or in application components. - ### Registering providers in an NgModule - Here's our AppModule where we register a `UserService` and an `APP_CONFIG` provider. + a#register-providers-ngmodule + :marked + ### Registering providers in an _NgModule_ + Here's the `AppModule` that registers two providers, `UserService` and an `APP_CONFIG` provider, + in its `providers` !{_array}. - var app_module_ts = 'src/app/app.module.ts'; - +makeExcerpt(app_module_ts + ' (excerpt)', 'ngmodule', app_module_ts, { otl: /(providers:)/ }) - //- The preferred approach is to register application providers in application components. - //- Because the `HeroService` is used within the *Heroes* feature area — - //- and nowhere else — the ideal place to register it is in the top-level `HeroesComponent`. + +makeExcerpt(app_module_ts + ' (excerpt)', 'ngmodule', app_module_ts) +:marked + Because the `HeroService` is used _only_ within the `HeroesComponent` + and its subcomponents, the top-level `HeroesComponent` is the ideal + place to register it. +a#register-providers-component :marked ### Registering providers in a component - Here's a revised `HeroesComponent` that registers the `HeroService`. + Here's a revised `HeroesComponent` that registers the `HeroService` in its `providers` !{_array}. -- var stylePattern = { otl: /(providers:[^,]+),/ }; +makeExample('src/app/heroes/heroes.component.1.ts', 'full', 'src/app/heroes/heroes.component.ts', stylePattern)(format='.') block ngmodule-vs-component + a#ngmodule-vs-comp :marked - ### When to use the NgModule and when an application component? + ### When to use _NgModule_ versus an application component - On the one hand, a provider in an NgModule is registered in the root injector. That means that every provider - registered within an NgModule will be accessible in the _entire application_. + On the one hand, a provider in an `NgModule` is registered in the root injector. That means that every provider + registered within an `NgModule` will be accessible in the _entire application_. - On the other hand, a provider registered in an application component is available only on that component and all its children. + On the other hand, a provider registered in an application component is available only on + that component and all its children. - We want the `APP_CONFIG` service to be available all across the application, but a `HeroService` is only used within the *Heroes* - feature area and nowhere else. + Here, the `APP_CONFIG` service needs to be available all across the application, so it's + registered in the `AppModule` `@NgModule` `providers` !{_array}. + But since the `HeroService` is only used within the *Heroes* + feature area and nowhere else, it makes sense to register it in + the `HeroesComponent`. .l-sub-section :marked - Also see *"Should I add app-wide providers to the root `AppModule` or the root `AppComponent`?"* in the [NgModule FAQ](../cookbook/ngmodule-faq.html#q-root-component-or-module). + Also see *"Should I add app-wide providers to the root `AppModule` or + the root `AppComponent`?"* in the [NgModule FAQ](../cookbook/ngmodule-faq.html#q-root-component-or-module). +a#prep-for-injection :marked - ### Preparing the HeroListComponent for injection + ### Preparing the _HeroListComponent_ for injection The `HeroListComponent` should get heroes from the injected `HeroService`. Per the dependency injection pattern, the component must ask for the service in its - constructor, [as we explained earlier](#ctor-injection). + constructor, [as discussed earlier](#ctor-injection). It's a small change: +makeTabs( @@ -330,38 +365,43 @@ block ngmodule-vs-component :marked ### Implicit injector creation - When we introduced the idea of an injector above, we showed how to - use it to create a new `Car`. Here we also show how such an injector - would be explicitly created: + You saw how to use an injector to create a new + `Car` earlier in this guide. + You _could_ create such an injector + explicitly: +makeExample('dependency-injection/ts/src/app/car/car-injector.ts','injector-create-and-call')(format=".") :marked - We won't find code like that in the Tour of Heroes or any of our other samples. - We *could* write code that [explicitly creates an injector](#explicit-injector) if we *had* to, but we rarely do. + You won't find code like that in the Tour of Heroes or any of the other + documentation samples. + You *could* write code that [explicitly creates an injector](#explicit-injector) if you *had* to, + but it's not always the best choice. Angular takes care of creating and calling injectors - when it creates components for us — whether through HTML markup, as in ``, + when it creates components for you—whether through HTML markup, as in ``, or after navigating to a component with the [router](./router.html). - If we let Angular do its job, we'll enjoy the benefits of automated dependency injection. + If you let Angular do its job, you'll enjoy the benefits of automated dependency injection. +a:#singleton-services :marked ### Singleton services Dependencies are singletons within the scope of an injector. - In our example, a single `HeroService` instance is shared among the + In this guide's example, a single `HeroService` instance is shared among the `HeroesComponent` and its `HeroListComponent` children. - However, Angular DI is an hierarchical injection + However, Angular DI is a hierarchical injection system, which means that nested injectors can create their own service instances. - Learn more about that in the [Hierarchical Injectors](./hierarchical-dependency-injection.html) chapter. + For more information, see [Hierarchical Injectors](./hierarchical-dependency-injection.html). +a#testing-the-component :marked ### Testing the component - We emphasized earlier that designing a class for dependency injection makes the class easier to test. - Listing dependencies as constructor parameters may be all we need to test application parts effectively. + Earlier you saw that designing a class for dependency injection makes the class easier to test. + Listing dependencies as constructor parameters may be all you need to test application parts effectively. - For example, we can create a new `HeroListComponent` with a mock service that we can manipulate + For example, you can create a new `HeroListComponent` with a mock service that you can manipulate under test: +makeExample('dependency-injection/ts/src/app/test.component.ts', 'spec')(format='.') @@ -370,14 +410,15 @@ block ngmodule-vs-component :marked Learn more in [Testing](./testing.html). +a#service-needs-service :marked ### When the service needs a service - Our `HeroService` is very simple. It doesn't have any dependencies of its own. + The `HeroService` is very simple. It doesn't have any dependencies of its own. What if it had a dependency? What if it reported its activities through a logging service? - We'd apply the same *constructor injection* pattern, + You'd apply the same *constructor injection* pattern, adding a constructor that takes a `Logger` parameter. Here is the revision compared to the original. @@ -391,45 +432,47 @@ block ngmodule-vs-component :marked The constructor now asks for an injected instance of a `Logger` and stores it in a private property called `#{_priv}logger`. - We call that property within our `getHeroes` method when anyone asks for heroes. + You call that property within the `getHeroes()` method when anyone asks for heroes. - var injUrl = '../api/core/index/Injectable-decorator.html'; -h3#injectable Why @Injectable()? +a#injectable :marked + ### Why _@Injectable()_? + **@Injectable()** marks a class as available to an - injector for instantiation. Generally speaking, an injector will report an + injector for instantiation. Generally speaking, an injector reports an error when trying to instantiate a class that is not marked as `@Injectable()`. block injectable-not-always-needed-in-ts .l-sub-section :marked - As it happens, we could have omitted `@Injectable()` from our first + As it happens, you could have omitted `@Injectable()` from the first version of `HeroService` because it had no injected parameters. - But we must have it now that our service has an injected dependency. - We need it because Angular requires constructor parameter metadata + But you must have it now that the service has an injected dependency. + You need it because Angular requires constructor parameter metadata in order to inject a `Logger`. .callout.is-helpful header Suggestion: add @Injectable() to every service class :marked - We recommend adding `@Injectable()` to every service class, even those that don't have dependencies + Consider adding `@Injectable()` to every service class, even those that don't have dependencies and, therefore, do not technically require it. Here's why: ul(style="font-size:inherit") - li Future proofing: No need to remember @Injectable() when we add a dependency later. - li Consistency: All services follow the same rules, and we don't have to wonder why #{_a} #{_decorator} is missing. + li Future proofing: No need to remember @Injectable() when you add a dependency later. + li Consistency: All services follow the same rules, and you don't have to wonder why #{_a} #{_decorator} is missing. :marked Injectors are also responsible for instantiating components - like `HeroesComponent`. Why haven't we marked `HeroesComponent` as + like `HeroesComponent`. So why doesn't `HeroesComponent` have `@Injectable()`? - We *can* add it if we really want to. It isn't necessary because the + You *can* add it if you really want to. It isn't necessary because the `HeroesComponent` is already marked with `@Component`, and this - !{_decorator} class (like `@Directive` and `@Pipe`, which we'll learn about later) - is a subtype of Injectable. It is in - fact `Injectable` #{_decorator}s that + !{_decorator} class (like `@Directive` and `@Pipe`, which you learn about later) + is a subtype of @Injectable(). It is in + fact `@Injectable()` #{_decorator}s that identify a class as a target for instantiation by an injector. +ifDocsFor('ts') @@ -447,7 +490,7 @@ block injectable-not-always-needed-in-ts for _every class with at least one decorator_. While any decorator will trigger this effect, mark the service class with the - Injectable #{_decorator} + @Injectable() #{_decorator} to make the intent clear. .callout.is-critical @@ -455,17 +498,17 @@ block injectable-not-always-needed-in-ts block always-include-paren :marked Always write `@Injectable()`, not just `@Injectable`. - Our application will fail mysteriously if we forget the parentheses. + The application will fail mysteriously if you forget the parentheses. .l-main-section#logger-service :marked ## Creating and registering a logger service - We're injecting a logger into our `HeroService` in two steps: + Inject a logger into `HeroService` in two steps: 1. Create the logger service. 1. Register it with the application. - Our logger service is quite simple: + The logger service is quite simple: +makeExample('src/app/logger.service.ts') @@ -473,24 +516,24 @@ block real-logger //- N/A :marked - We're likely to need the same logger service everywhere in our application, - so we put it in the project's `#{_appDir}` folder, and - we register it in the `providers` #{_array} of our application !{_moduleVsComp}, `!{_AppModuleVsAppComp}`. + You're likely to need the same logger service everywhere in your application, + so put it in the project's `#{_appDir}` folder and + register it in the `providers` #{_array} of the application !{_moduleVsComp}, `!{_AppModuleVsAppComp}`. +makeExcerpt('src/app/providers.component.ts (excerpt)', 'providers-logger','src/app/app.module.ts') :marked - If we forget to register the logger, Angular throws an exception when it first looks for the logger: + If you forget to register the logger, Angular throws an exception when it first looks for the logger: code-example(format="nocode"). EXCEPTION: No provider for Logger! (HeroListComponent -> HeroService -> Logger) :marked - That's Angular telling us that the dependency injector couldn't find the *provider* for the logger. + That's Angular telling you that the dependency injector couldn't find the *provider* for the logger. It needed that provider to create a `Logger` to inject into a new `HeroService`, which it needed to create and inject into a new `HeroListComponent`. - The chain of creations started with the `Logger` provider. *Providers* are the subject of our next section. + The chain of creations started with the `Logger` provider. *Providers* are the subject of the next section. .l-main-section#providers :marked @@ -500,23 +543,23 @@ code-example(format="nocode"). The injector relies on **providers** to create instances of the services that the injector injects into components and other services. - We must register a service *provider* with the injector, or it won't know how to create the service. + You must register a service *provider* with the injector, or it won't know how to create the service. - Earlier we registered the `Logger` service in the `providers` #{_array} of the metadata for the `AppModule` like this: + Earlier you registered the `Logger` service in the `providers` #{_array} of the metadata for the `AppModule` like this: +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-logger') - var implements = _docsFor == 'dart' ? 'implements' : 'looks and behaves like a ' - var objectlike = _docsFor == 'dart' ? '' : 'an object that behaves like ' -- var loggerlike = _docsFor == 'dart' ? '' : 'We could provide a logger-like object. ' +- var loggerlike = _docsFor == 'dart' ? '' : 'You could provide a logger-like object. ' :marked There are many ways to *provide* something that #{implements} `Logger`. The `Logger` class itself is an obvious and natural provider. But it's not the only way. - We can configure the injector with alternative providers that can deliver #{objectlike} a `Logger`. - We could provide a substitute class. #{loggerlike} - We could give it a provider that calls a logger factory function. + You can configure the injector with alternative providers that can deliver #{objectlike} a `Logger`. + You could provide a substitute class. #{loggerlike} + You could give it a provider that calls a logger factory function. Any of these approaches might be a good choice under the right circumstances. What matters is that the injector has a provider to go to when it needs a `Logger`. @@ -528,7 +571,7 @@ code-example(format="nocode"). ### The *Provider* class !{_andProvideFn} :marked - We wrote the `providers` #{_array} like this: + You wrote the `providers` #{_array} like this: +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-1') @@ -547,14 +590,14 @@ block provider-ctor-args and registering the provider. The second is a !{_secondParam}, - which we can think of as a *recipe* for creating the dependency value. - There are many ways to create dependency values ... and many ways to write a recipe. + which you can think of as a *recipe* for creating the dependency value. + There are many ways to create dependency values just as there are many ways to write a recipe. #class-provider :marked ### Alternative class providers - Occasionally we'll ask a different class to provide the service. + Occasionally you'll ask a different class to provide the service. The following code tells the injector to return a `BetterLogger` when something asks for the `Logger`. @@ -563,45 +606,46 @@ block provider-ctor-args block dart-diff-const-metadata //- N/A +a#class-provider-dependencies :marked ### Class provider with dependencies Maybe an `EvenBetterLogger` could display the user name in the log message. This logger gets the user from the injected `UserService`, - which happens also to be injected at the application level. + which is also injected at the application level. +makeExample('dependency-injection/ts/src/app/providers.component.ts','EvenBetterLogger')(format='.') :marked - Configure it like we did `BetterLogger`. + Configure it like `BetterLogger`. +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-5')(format=".") +a#aliased-class-providers :marked ### Aliased class providers Suppose an old component depends upon an `OldLogger` class. `OldLogger` has the same interface as the `NewLogger`, but for some reason - we can't update the old component to use it. + you can't update the old component to use it. When the *old* component logs a message with `OldLogger`, - we want the singleton instance of `NewLogger` to handle it instead. + you'd like the singleton instance of `NewLogger` to handle it instead. The dependency injector should inject that singleton instance when a component asks for either the new or the old logger. The `OldLogger` should be an alias for `NewLogger`. - We certainly do not want two different `NewLogger` instances in our app. - Unfortunately, that's what we get if we try to alias `OldLogger` to `NewLogger` with `useClass`. + You certainly do not want two different `NewLogger` instances in your app. + Unfortunately, that's what you get if you try to alias `OldLogger` to `NewLogger` with `useClass`. +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-6a')(format=".") :marked The solution: alias with the `useExisting` option. -- var stylePattern = { otl: /(useExisting: \w*)/gm }; +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-6b', '', stylePattern)(format=".") -#value-provider +a#value-provider :marked ### Value providers @@ -614,10 +658,9 @@ block dart-diff-const-metadata-ctor +makeExample('dependency-injection/ts/src/app/providers.component.ts','silent-logger')(format=".") :marked - Then we register a provider with the `useValue` option, + Then you register a provider with the `useValue` option, which makes this object play the logger role. -- var stylePattern = { otl: /(useValue: \w*)/gm }; +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-7', '', stylePattern)(format=".") :marked @@ -629,48 +672,45 @@ block dart-diff-const-metadata-ctor :marked ### Factory providers - Sometimes we need to create the dependent value dynamically, - based on information we won't have until the last possible moment. + Sometimes you need to create the dependent value dynamically, + based on information you won't have until the last possible moment. Maybe the information changes repeatedly in the course of the browser session. Suppose also that the injectable service has no independent access to the source of this information. This situation calls for a **factory provider**. - Let's illustrate by adding a new business requirement: - the HeroService must hide *secret* heroes from normal users. + To illustrate the point, add a new business requirement: + the `HeroService` must hide *secret* heroes from normal users. Only authorized users should see secret heroes. Like the `EvenBetterLogger`, the `HeroService` needs a fact about the user. It needs to know if the user is authorized to see secret heroes. That authorization can change during the course of a single application session, - as when we log in a different user. + as when you log in a different user. - Unlike `EvenBetterLogger`, we can't inject the `UserService` into the `HeroService`. + Unlike `EvenBetterLogger`, you can't inject the `UserService` into the `HeroService`. The `HeroService` won't have direct access to the user information to decide who is authorized and who is not. -.l-sub-section - :marked - Why? We don't know either. Stuff like this happens. - :marked - Instead the `HeroService` constructor takes a boolean flag to control display of secret heroes. + Instead, the `HeroService` constructor takes a boolean flag to control display of secret heroes. +makeExample('dependency-injection/ts/src/app/heroes/hero.service.ts','internals', 'src/app/heroes/hero.service.ts (excerpt)')(format='.') :marked - We can inject the `Logger`, but we can't inject the boolean `isAuthorized`. - We'll have to take over the creation of new instances of this `HeroService` with a factory provider. + You can inject the `Logger`, but you can't inject the boolean `isAuthorized`. + You'll have to take over the creation of new instances of this `HeroService` with a factory provider. A factory provider needs a factory function: +makeExample('dependency-injection/ts/src/app/heroes/hero.service.provider.ts','factory', 'src/app/heroes/hero.service.provider.ts (excerpt)')(format='.') :marked - Although the `HeroService` has no access to the `UserService`, our factory function does. + Although the `HeroService` has no access to the `UserService`, the factory function does. - We inject both the `Logger` and the `UserService` into the factory provider and let the injector pass them along to the factory function: + You inject both the `Logger` and the `UserService` into the factory provider + and let the injector pass them along to the factory function: +makeExample('dependency-injection/ts/src/app/heroes/hero.service.provider.ts','provider', 'src/app/heroes/hero.service.provider.ts (excerpt)')(format='.') @@ -686,15 +726,14 @@ block dart-diff-const-metadata-ctor - var exportedvar = _docsFor == 'dart' ? 'constant' : 'exported variable' - var variable = _docsFor == 'dart' ? 'constant' : 'variable' :marked - Notice that we captured the factory provider in #{_an} #{exportedvar}, `heroServiceProvider`. + Notice that you captured the factory provider in #{_an} #{exportedvar}, `heroServiceProvider`. This extra step makes the factory provider reusable. - We can register our `HeroService` with this #{variable} wherever we need it. + You can register the `HeroService` with this #{variable} wherever you need it. - In our sample, we need it only in the `HeroesComponent`, + In this sample, you need it only in the `HeroesComponent`, where it replaces the previous `HeroService` registration in the metadata `providers` #{_array}. - Here we see the new and the old implementation side-by-side: + Here you see the new and the old implementation side-by-side: -- var stylePattern = { otl: /(providers.*),$/gm }; +makeTabs( `dependency-injection/ts/src/app/heroes/heroes.component.ts, dependency-injection/ts/src/app/heroes/heroes.component.1.ts`, @@ -707,32 +746,33 @@ block dart-diff-const-metadata-ctor :marked ## Dependency injection tokens - When we register a provider with an injector, we associate that provider with a dependency injection token. + When you register a provider with an injector, you associate that provider with a dependency injection token. The injector maintains an internal *token-provider* map that it references when asked for a dependency. The token is the key to the map. In all previous examples, the dependency value has been a class *instance*, and the class *type* served as its own lookup key. - Here we get a `HeroService` directly from the injector by supplying the `HeroService` type as the token: + Here you get a `HeroService` directly from the injector by supplying the `HeroService` type as the token: +makeExample('dependency-injection/ts/src/app/injector.component.ts','get-hero-service')(format='.') :marked - We have similar good fortune when we write a constructor that requires an injected class-based dependency. - We define a constructor parameter with the `HeroService` class type, - and Angular knows to inject the + You have similar good fortune when you write a constructor that requires an injected class-based dependency. + When you define a constructor parameter with the `HeroService` class type, + Angular knows to inject the service associated with that `HeroService` class token: +makeExample('dependency-injection/ts/src/app/heroes/hero-list.component.ts', 'ctor-signature') :marked - This is especially convenient when we consider that most dependency values are provided by classes. + This is especially convenient when you consider that most dependency values are provided by classes. //- TODO: if function injection is useful explain or illustrate why. +a#non-class-dependencies :marked ### Non-class dependencies p - | What if the dependency value isn't a class? Sometimes the thing we want to inject is a + | What if the dependency value isn't a class? Sometimes the thing you want to inject is a block non-class-dep-eg span string, function, or object. p @@ -746,35 +786,36 @@ p +makeExample('dependency-injection/ts/src/app/app.config.ts','config','src/app/app-config.ts (excerpt)')(format='.') :marked - We'd like to make this configuration object available for injection. - We know we can register an object with a [value provider](#value-provider). + What if you'd like to make this configuration object available for injection? + You know you can register an object with a [value provider](#value-provider). block what-should-we-use-as-token :marked - But what should we use as the token? - We don't have a class to serve as a token. + But what should you use as the token? + You don't have a class to serve as a token. There is no `AppConfig` class. .l-sub-section#interface :marked ### TypeScript interfaces aren't valid tokens - The `HERO_DI_CONFIG` constant has an interface, `AppConfig`. Unfortunately, we + The `HERO_DI_CONFIG` constant has an interface, `AppConfig`. Unfortunately, you cannot use a TypeScript interface as a token: +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-9-interface')(format=".") +makeExample('dependency-injection/ts/src/app/providers.component.ts','provider-9-ctor-interface')(format=".") :marked - That seems strange if we're used to dependency injection in strongly typed languages, where + That seems strange if you're used to dependency injection in strongly typed languages, where an interface is the preferred dependency lookup key. - It's not Angular's fault. An interface is a TypeScript design-time artifact. JavaScript doesn't have interfaces. + It's not Angular's doing. An interface is a TypeScript design-time artifact. JavaScript doesn't have interfaces. The TypeScript interface disappears from the generated JavaScript. There is no interface type information left for Angular to find at runtime. //- FIXME update once https://github.com/dart-lang/angular2/issues/16 is addressed. - var opaquetoken = _docsFor == 'dart' ? 'OpaqueToken' : 'OpaqueToken' +a#opaquetoken :marked - ### OpaqueToken + ### _OpaqueToken_ One solution to choosing a provider token for non-class dependencies is to define and use an !{opaquetoken}. @@ -783,12 +824,12 @@ block what-should-we-use-as-token +makeExample('dependency-injection/ts/src/app/app.config.ts','token')(format='.') :marked - We register the dependency provider using the `OpaqueToken` object: + Register the dependency provider using the `OpaqueToken` object: +makeExample('dependency-injection/ts/src/app/providers.component.ts','providers-9')(format=".") :marked - Now we can inject the configuration object into any constructor that needs it, with + Now you can inject the configuration object into any constructor that needs it, with the help of an `@Inject` #{_decorator}: +makeExample('dependency-injection/ts/src/app/app.component.2.ts','ctor')(format=".") @@ -801,7 +842,7 @@ block what-should-we-use-as-token block dart-map-alternative :marked - Or we can provide and inject the configuration object in an ngModule like `AppModule`. + Aternatively, you can provide and inject the configuration object in an ngModule like `AppModule`. +makeExcerpt('src/app/app.module.ts','ngmodule-providers') @@ -809,9 +850,9 @@ block dart-map-alternative :marked ## Optional dependencies - Our `HeroService` *requires* a `Logger`, but what if it could get by without - a logger? - We can tell Angular that the dependency is optional by annotating the + The `HeroService` *requires* a `Logger`, but what if it could get by without + a `logger`? + You can tell Angular that the dependency is optional by annotating the constructor argument with `@Optional()`: +ifDocsFor('ts') @@ -819,29 +860,29 @@ block dart-map-alternative +makeExample('dependency-injection/ts/src/app/providers.component.ts','provider-10-ctor', '')(format='.') :marked - When using `@Optional()`, our code must be prepared for a null value. If we - don't register a logger somewhere up the line, the injector will set the + When using `@Optional()`, your code must be prepared for a null value. If you + don't register a `logger` somewhere up the line, the injector will set the value of `logger` to null. .l-main-section :marked ## Summary - We learned the basics of Angular dependency injection in this chapter. - We can register various kinds of providers, - and we know how to ask for an injected object (such as a service) by + You learned the basics of Angular dependency injection in this page. + You can register various kinds of providers, + and you know how to ask for an injected object (such as a service) by adding a parameter to a constructor. - Angular dependency injection is more capable than we've described. - We can learn more about its advanced features, beginning with its support for - nested injectors, in the - [Hierarchical Dependency Injection](hierarchical-dependency-injection.html) chapter. + Angular dependency injection is more capable than this guide has described. + You can learn more about its advanced features, beginning with its support for + nested injectors, in + [Hierarchical Dependency Injection](hierarchical-dependency-injection.html). .l-main-section#explicit-injector :marked ## Appendix: Working with injectors directly - We rarely work directly with an injector, but + Developers rarely work directly with an injector, but here's an `InjectorComponent` that does. +makeExample('dependency-injection/ts/src/app/injector.component.ts', 'injector', 'src/app/injector.component.ts') @@ -853,24 +894,23 @@ block dart-map-alternative The component then asks the injected injector for the services it wants. Note that the services themselves are not injected into the component. - They are retrieved by calling `injector.get`. + They are retrieved by calling `injector.get()`. - The `get` method throws an error if it can't resolve the requested service. - We can call `get` with a second parameter (the value to return if the service is not found) - instead, which we do in one case - to retrieve a service (`ROUS`) that isn't registered with this or any ancestor injector. + The `get()` method throws an error if it can't resolve the requested service. + You can call `get()` with a second parameter, which is the value to return if the service + is not found. Angular can't find the service if it's not registered with this or any ancestor injector. .l-sub-section :marked - The technique we just described is an example of the + The technique is an example of the [service locator pattern](https://en.wikipedia.org/wiki/Service_locator_pattern). - We **avoid** this technique unless we genuinely need it. - It encourages a careless grab-bag approach such as we see here. + **Avoid** this technique unless you genuinely need it. + It encourages a careless grab-bag approach such as you see here. It's difficult to explain, understand, and test. - We can't know by inspecting the constructor what this class requires or what it will do. + You can't know by inspecting the constructor what this class requires or what it will do. It could acquire services from any ancestor component, not just its own. - We're forced to spelunk the implementation to discover what it does. + You're forced to spelunk the implementation to discover what it does. Framework developers may take this approach when they must acquire services generically and dynamically. @@ -878,20 +918,20 @@ block dart-map-alternative +ifDocsFor('ts') .l-main-section#one-class-per-file :marked - ## Appendix: Why we recommend one class per file + ## Appendix: Why have one class per file Having multiple classes in the same file is confusing and best avoided. Developers expect one class per file. Keep them happy. - If we scorn this advice and, say, - combine our `HeroService` class with the `HeroesComponent` in the same file, - **define the component last!** - If we define the component before the service, - we'll get a runtime null reference error. + If you combine the `HeroService` class with + the `HeroesComponent` in the same file, + **define the component last**. + If you define the component before the service, + you'll get a runtime null reference error. .l-sub-section :marked - We actually can define the component first with the help of the `forwardRef()` method as explained + You actually can define the component first with the help of the `forwardRef()` method as explained in this [blog post](http://blog.thoughtram.io/angular/2015/09/03/forward-references-in-angular-2.html). But why flirt with trouble? Avoid the problem altogether by defining components and services in separate files.