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

defineInjectable

Construct an InjectableDef which defines how a token will be constructed by the DI system, and in which injectors (if any) it will be available.

See more...

defineInjectable<T>(opts: { providedIn?: Type<any> | 'root' | 'any' | null; factory: () => T; }): never
      
      defineInjectable<T>(opts: {
    providedIn?: Type<any> | 'root' | 'any' | null;
    factory: () => T;
}): never
    

Parameters

opts

Type: { providedIn?: Type | 'root' | 'any' | null; factory: () => T; }.

Returns

never

Description

This should be assigned to a static ngInjectableDef field on a type, which will then be an InjectableType.

Options:

  • providedIn determines which injectors will include the injectable, by either associating it with an @NgModule or other InjectorType, or by specifying that this injectable should be provided in the 'root' injector, which will be the application-level injector in most apps.
  • factory gives the zero argument function which will create an instance of the injectable. The factory can call inject to access the Injector and request injection of dependencies.