Cookies concent notice

This site uses cookies from Google to deliver its services and to analyze traffic.
Learn more
Skip to main content
This site is no longer updated.Head to Angular.devHome
/

This is the archived documentation for Angular v17. Please visit angular.dev to see this page for the current version of Angular.

Injectable

Decorator that marks a class as available to be provided and injected as a dependency.

OptionDescription
providedIn?

Determines which injectors will provide the injectable.

See also

Options

Determines which injectors will provide the injectable.

      
      providedIn?: Type<any> | 'root' | 'platform' | 'any' | null
    
  • Type<any> - associates the injectable with an @NgModule or other InjectorType. This option is DEPRECATED.
  • 'null' : Equivalent to undefined. The injectable is not provided in any scope automatically and must be added to a providers array of an @NgModule, @Component or @Directive.

The following options specify that this injectable should be provided in one of the following injectors:

  • 'root' : The application-level injector in most apps.
  • 'platform' : A special singleton platform injector shared by all applications on the page.
  • 'any' : Provides a unique instance in each lazy loaded module while all eagerly loaded modules share one instance. This option is DEPRECATED.

Usage notes

Marking a class with @Injectable ensures that the compiler will generate the necessary metadata to create the class's dependencies when the class is injected.

The following example shows how a service class is properly marked so that a supporting service can be injected upon creation.

      
      @Injectable()
class UsefulService {}

@Injectable()
class NeedsService {
  constructor(public service: UsefulService) {}
}

const injector = Injector.create({
  providers: [
    {provide: NeedsService, deps: [UsefulService]},
    {provide: UsefulService, deps: []},
  ],
});
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);