Injector

class

npm Package @angular/core
Module import { Injector } from '@angular/core';
Source core/src/di/injector.ts

Injector interface

Overview

class Injector {
  static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND
  static NULL: Injector
  get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T
}

How To Use

const injector: Injector = ...;
injector.get(...);

Description

For more details, see the "Dependency Injection Guide".

Example

const injector: Injector =
    ReflectiveInjector.resolveAndCreate([{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 = ReflectiveInjector.resolveAndCreate([]);
expect(injector.get(Injector)).toBe(injector);

Subclasses

  • ReflectiveInjector
  • TestBed

Static Members

static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND

static NULL: Injector

Members

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

Retrieves an instance from the injector based on the provided token. If not found:

  • Throws an error if no notFoundValue that is not equal to Injector.THROW_IF_NOT_FOUND is given
  • Returns the notFoundValue otherwise
Overloads

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

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v4.angular.io/api/core/Injector