Injector
Concrete injectors implement this interface.
abstract class Injector {
static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND
static NULL: Injector
static ngInjectableDef: defineInjectable({...})
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
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 ngInjectableDef: defineInjectable({
providedIn: 'any' as any,
factory: () => inject(INJECTOR)
})
|
Static methods
Create a new Injector which is configure using |
||||||
Deprecated from v5 use the new signature Injector.create(options) Parameters
Returns |
||||||
Usage NotesExample
|
Methods
Retrieves an instance from the injector based on the provided token. |
|||||||||
Parameters
Returns
Throws
|
|||||||||
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);