Injector

export class Injector

exported from angular2/core defined in angular2/src/core/di/injector.ts (line 393)

A dependency injection container used for instantiating objects and resolving dependencies.

An Injector is a replacement for a new operator, which can automatically resolve the constructor dependencies.

In typical use, application code asks for the dependencies in the constructor and they are resolved by the Injector.

Example (live demo)

The following example creates an Injector configured to create Engine and Car.

@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = Injector.resolveAndCreate([Car, Engine]); var car = injector.get(Car); expect(car instanceof Car).toBe(true); expect(car.engine instanceof Engine).toBe(true);

Notice, we don't use the new operator because we explicitly want to have the Injector resolve all of the object's dependencies automatically.

Constructor

constructor(_proto: any, _parent?: Injector, _depProvider?: any, _debugContext?: Function)

Private

Members

get(token: any) : any

Retrieves an instance from the injector based on the provided token. Throws NoProviderError if not found.

(live demo)

var injector = Injector.resolveAndCreate([ provide("validToken", {useValue: "Value"}) ]); expect(injector.get("validToken")).toEqual("Value"); expect(() => injector.get("invalidToken")).toThrowError();

Injector returns itself when given Injector as a token.

var injector = Injector.resolveAndCreate([]); expect(injector.get(Injector)).toBe(injector);

getOptional(token: any) : any

Retrieves an instance from the injector based on the provided token. Returns null if not found.

(live demo)

var injector = Injector.resolveAndCreate([ provide("validToken", {useValue: "Value"}) ]); expect(injector.getOptional("validToken")).toEqual("Value"); expect(injector.getOptional("invalidToken")).toBe(null);

Injector returns itself when given Injector as a token.

var injector = Injector.resolveAndCreate([]); expect(injector.getOptional(Injector)).toBe(injector);

parent : Injector

Parent of this injector.

(live demo)

var parent = Injector.resolveAndCreate([]); var child = parent.resolveAndCreateChild([]); expect(child.parent).toBe(parent);

resolveAndCreateChild(providers: Array<Type | Provider | any[]>) : Injector

Resolves an array of providers and creates a child injector from those providers.

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

(live demo)

class ParentProvider {} class ChildProvider {} var parent = Injector.resolveAndCreate([ParentProvider]); var child = parent.resolveAndCreateChild([ChildProvider]); expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));

This function is slower than the corresponding createChildFromResolved because it needs to resolve the passed-in providers first. See Injector and Injector.

createChildFromResolved(providers: ResolvedProvider[]) : Injector

Creates a child injector from previously resolved providers.

This API is the recommended way to construct injectors in performance-sensitive parts.

(live demo)

class ParentProvider {} class ChildProvider {} var parentProviders = Injector.resolve([ParentProvider]); var childProviders = Injector.resolve([ChildProvider]); var parent = Injector.fromResolvedProviders(parentProviders); var child = parent.createChildFromResolved(childProviders); expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true); expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true); expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));

resolveAndInstantiate(provider: Type | Provider) : any

Resolves a provider and instantiates an object in the context of the injector.

The created object does not get cached by the injector.

(live demo)

@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = Injector.resolveAndCreate([Engine]); var car = injector.resolveAndInstantiate(Car); expect(car.engine).toBe(injector.get(Engine)); expect(car).not.toBe(injector.resolveAndInstantiate(Car));

instantiateResolved(provider: ResolvedProvider) : any

Instantiates an object using a resolved provider in the context of the injector.

The created object does not get cached by the injector.

(live demo)

@Injectable() class Engine { } @Injectable() class Car { constructor(public engine:Engine) {} } var injector = Injector.resolveAndCreate([Engine]); var carProvider = Injector.resolve([Car])[0]; var car = injector.instantiateResolved(carProvider); expect(car.engine).toBe(injector.get(Engine)); expect(car).not.toBe(injector.instantiateResolved(carProvider));

displayName : string

Not Yet Documented

toString() : string

Not Yet Documented