This is the archived documentation for Angular v6. Please visit angular.io to see documentation for the current version of Angular.

Injector

Concrete injectors implement this interface.

See more...

      
      abstract class Injector {
  static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND
  static NULL: Injector
  static ngInjectableDef: defineInjectable({...})
  static create(options: StaticProvider[] | {...}, parent?: Injector): Injector
  abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
}
    

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 StaticProviders.

static create(providers: StaticProvider[], parent?: Injector): Injector
      
      static create(providers: StaticProvider[], parent?: Injector): Injector
    

Deprecated from v5 use the new signature Injector.create(options)

Parameters

providers

Type: StaticProvider[].

parent

Type: Injector.

Optional. Default is undefined.

Returns

Injector

static create(options: { providers: StaticProvider[]; parent?: Injector; name?: string; }): Injector
      
      static create(options: {
    providers: StaticProvider[];
    parent?: Injector;
    name?: string;
}): Injector
    

Parameters

options

Type: { providers: StaticProvider[]; parent?: Injector; name?: string; }.

Returns

Injector

Example

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);
      
      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);
    

Methods

Retrieves an instance from the injector based on the provided token.

abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
      
      abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
    

Parameters

token

Type: Type | InjectionToken.

notFoundValue

Type: T.

Optional. Default is undefined.

flags

Type: InjectFlags.

Optional. Default is undefined.

Returns

T: The instance from the injector if defined, otherwise the notFoundValue.

Throws

Error When the notFoundValue is undefined or Injector.THROW_IF_NOT_FOUND.

abstract get(token: any, notFoundValue?: any): any
      
      abstract get(token: any, notFoundValue?: any): any
    

Deprecated from v4.0.0 use Type or InjectionToken

Parameters

token

Type: any.

notFoundValue

Type: any.

Optional. Default is undefined.

Returns

any

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');
      
      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);
      
      const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);