Injection Beyond Classes

So far the only types that injection has been used for have been classes, butAngular is not limited to injecting classes. The concept of providers wasalso briefly touched upon.

So far providers have been used with Angular's @NgModule meta in anarray. providers have also all been class identifiers. Angular letsprogrammers specify providers with a more verbose "recipe". This is done withby providing Angular an Object literal ({}):

  1. import { NgModule } from '@angular/core';
  2. import { App } from './containers/app'; // hypothetical app component
  3. import { ChatWidget } from './components/chat-widget';
  4. @NgModule({
  5. providers: [ { provide: ChatWidget, useClass: ChatWidget } ],
  6. })
  7. export class DiExample {};

This example is yet another example that provides a class, but it does so withAngular's longer format.

This long format is really handy. If the programmer wanted to switch outChatWidget implementations, for example to allow for a MockChatWidget, they coulddo this easily:

  1. import { NgModule } from '@angular/core';
  2. import { App } from './containers/app'; // hypothetical app component
  3. import { ChatWidget } from './components/chat-widget';
  4. import { MockChatWidget } from './components/mock-chat-widget';
  5. @NgModule({
  6. providers: [ { provide: ChatWidget, useClass: MockChatWidget } ],
  7. })
  8. export class DiExample {};

The best part of this implementation swap is that the injection system knowshow to build MockChatWidget, and will sort all of that out.

The injector can use more than classes though. useValue and useFactory aretwo other examples of provider "recipes" that Angular can use. For example:

  1. import { NgModule } from '@angular/core';
  2. import { App } from './containers/app'; // hypothetical app component
  3. const randomFactory = () => { return Math.random(); };
  4. @NgModule({
  5. providers: [ { provide: 'Random', useFactory: randomFactory } ],
  6. })
  7. export class DiExample {};

In the hypothetical app component, 'Random' could be injected like:

  1. import { Component, Inject, provide } from '@angular/core';
  2. @Component({
  3. selector: 'app-root',
  4. template: `Random: {{ value }}`
  5. })
  6. export class AppCompoennt {
  7. value: number;
  8. constructor(@Inject('Random') r) {
  9. this.value = r;
  10. }
  11. }

View Example

One important note is that 'Random' is in quotes, both in the providefunction and in the consumer. This is because as a factory we have no Randomidentifier anywhere to access.

The above example uses Angular's useFactory recipe. When Angular is toldto provide things using useFactory, Angular expects the provided value to bea function. Sometimes functions and classes are even more than what's needed.Angular has a "recipe" called useValue for these cases that works almostexactly the same:

  1. import { NgModule } from '@angular/core';
  2. import { AppComponent } from './containers/app.component'; // hypothetical app component
  3. @NgModule({
  4. providers: [ { provide: 'Random', useValue: Math.random() } ],
  5. })
  6. export class DiExample {};

View Example

In this case, the product of Math.random is assigned to the useValueproperty passed to the provider.

原文: https://angular-2-training-book.rangle.io/handout/di/angular2/injection_beyond_classes.html