Injector
class
Concrete injectors implement this interface.
abstract class Injector {
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND
static NULL: Injector
static create(options: StaticProvider[] | { providers: StaticProvider[]; parent?: Injector; name?: string; }, parent?: Injector): Injector
abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
}
Subclasses
ReflectiveInjector
Description
For more details, see the "Dependency Injection Guide".
Static properties
Property | Description |
---|---|
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND
|
|
static NULL: Injector
|
Static methods
create() | ||||||
---|---|---|---|---|---|---|
Create a new Injector which is configure using StaticProvider s.
| ||||||
static create(providers: StaticProvider[], parent?: Injector): InjectorDeprecated from v5 use the new signature Injector.create(options)
Parameters
Returns
| ||||||
static create(options: { providers: StaticProvider[]; parent?: Injector; name?: string; }): InjectorParameters
Returns
| ||||||
Usage NotesExample
|
Methods
get() | |||||||||
---|---|---|---|---|---|---|---|---|---|
Retrieves an instance from the injector based on the provided token. | |||||||||
abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): TParameters
Returns
Throws
| |||||||||
abstract get(token: any, notFoundValue?: any): anyDeprecated from v4.0.0 use Type or InjectionToken
Parameters
Returns
|
Usage notes
Example
const injector: Injector =
Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
Injector
returns itself when given Injector
as a token:
const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);
© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v8.angular.io/api/core/Injector