export class 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)
Constructor
constructor(token: any, {useClass, useValue, useExisting, useFactory, deps, multi}: {
useClass?: Type,
useValue?: any,
useExisting?: any,
useFactory?: Function,
deps?: Object[],
multi?: boolean
})
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
Token used when retrieving this provider. Usually, it is a type Type
.
useClass : 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.
useValue
useValue
Binds a DI token to a value.
(live demo)
useExisting
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.
useFactory : Function
useFactory : Function
Binds a DI token to a function which computes the value.
(live demo)
Used in conjunction with dependencies.
dependencies : Object[]
dependencies : Object[]
Specifies a set of dependencies
(as token
s) which should be injected into the factory function.
(live demo)
Used in conjunction with useFactory
.
multi : boolean
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.
#
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"})
]);