Provider

export class Provider

exported from angular2/core defined in angular2/src/core/di/provider.ts (line 44)

Describes how the Injector should instantiate a given token.

See provide.

Example (live demo)

var injector = Injector.resolveAndCreate([ new Provider("message", { useValue: 'Hello' }) ]); expect(injector.get("message")).toEqual('Hello');

Constructor

constructor(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
  useClass?: Type,
  useValue?: any,
  useExisting?: any,
  useFactory?: Function,
  deps?: Object[],
  multi?: boolean
})

Not Yet Documented

Members

token

Token used when retrieving this provider. Usually, it is a type Type.

useClass : Type

Binds a DI token to an implementation class.

(live demo)

Because useExisting and useClass are often confused, the example contains both use cases for easy comparison.

class Vehicle {} class Car extends Vehicle {} var injectorClass = Injector.resolveAndCreate([ Car, new Provider(Vehicle, { useClass: Car }) ]); var injectorAlias = Injector.resolveAndCreate([ Car, new Provider(Vehicle, { useExisting: Car }) ]); expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car)); expect(injectorClass.get(Vehicle) instanceof Car).toBe(true); expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car)); expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true);

useValue

Binds a DI token to a value.

(live demo)

var injector = Injector.resolveAndCreate([ new Provider("message", { useValue: 'Hello' }) ]); expect(injector.get("message")).toEqual('Hello');

useExisting

Binds a DI token to an existing token.

Injector returns the same instance as if the provided token was used. This is in contrast to useClass where a separate instance of useClass is returned.

(live demo)

Because useExisting and useClass are often confused the example contains both use cases for easy comparison.

class Vehicle {} class Car extends Vehicle {} var injectorAlias = Injector.resolveAndCreate([ Car, new Provider(Vehicle, { useExisting: Car }) ]); var injectorClass = Injector.resolveAndCreate([ Car, new Provider(Vehicle, { useClass: Car }) ]); expect(injectorAlias.get(Vehicle)).toBe(injectorAlias.get(Car)); expect(injectorAlias.get(Vehicle) instanceof Car).toBe(true); expect(injectorClass.get(Vehicle)).not.toBe(injectorClass.get(Car)); expect(injectorClass.get(Vehicle) instanceof Car).toBe(true);

useFactory : Function

Binds a DI token to a function which computes the value.

(live demo)

var injector = Injector.resolveAndCreate([ new Provider(Number, { useFactory: () => { return 1+2; }}), new Provider(String, { useFactory: (value) => { return "Value: " + value; }, deps: [Number] }) ]); expect(injector.get(Number)).toEqual(3); expect(injector.get(String)).toEqual('Value: 3');

Used in conjunction with dependencies.

dependencies : Object[]

Specifies a set of dependencies (as tokens) which should be injected into the factory function.

(live demo)

var injector = Injector.resolveAndCreate([ new Provider(Number, { useFactory: () => { return 1+2; }}), new Provider(String, { useFactory: (value) => { return "Value: " + value; }, deps: [Number] }) ]); expect(injector.get(Number)).toEqual(3); expect(injector.get(String)).toEqual('Value: 3');

Used in conjunction with useFactory.

multi : boolean

Creates multiple providers matching the same token (a multi-provider).

Multi-providers are used for creating pluggable service, where the system comes with some default providers, and the user can register additional providers. The combination of the default providers and the additional providers will be used to drive the behavior of the system.

#

var injector = Injector.resolveAndCreate([ new Provider("Strings", { useValue: "String1", multi: true}), new Provider("Strings", { useValue: "String2", multi: true}) ]); expect(injector.get("Strings")).toEqual(["String1", "String2"]);

Multi-providers and regular providers cannot be mixed. The following will throw an exception:

var injector = Injector.resolveAndCreate([
  new Provider("Strings", { useValue: "String1", multi: true }),
  new Provider("Strings", { useValue: "String2"})
]);