class
Concrete injectors implement this interface. Injectors are configured with providers that associate dependencies of various types with injection tokens.
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> | AbstractType<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
} 'any'
| Property | Description |
|---|---|
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND | |
static NULL: Injector |
| create() | ||||||
|---|---|---|---|---|---|---|
|
providers | StaticProvider[] | |
parent | Injector | Optional. Default is |
Creates a new injector instance that provides one or more dependencies, according to a given type or types of StaticProvider.
static create(options: { providers: StaticProvider[]; parent?: Injector; name?: string; }): Injector options | object | An object with the following properties:
|
Injector: The new injector instance.
| get() | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Retrieves an instance from the injector based on the provided token. | |||||||||
|
token | Type | |
notFoundValue | T | Optional. Default is |
flags | InjectFlags | Optional. Default is |
T: The instance from the injector if defined, otherwise the notFoundValue.
Error When the notFoundValue is undefined or Injector.THROW_IF_NOT_FOUND.
abstract get(token: any, notFoundValue?: any): anyDeprecated from v4.0.0 use Type
token | any | |
notFoundValue | any | Optional. Default is |
any
The following example creates a service injector instance.
class Square {
name = 'square';
}
const injector = Injector.create({providers: [{provide: Square, deps: []}]});
const shape: Square = injector.get(Square);
expect(shape.name).toEqual('square');
expect(shape instanceof Square).toBe(true); 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–2021 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v11.angular.io/api/core/Injector-0