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

Injectable

A marker metadata that marks a class as available to Injector for creation.

See more...

OptionDescription
providedIn

Description

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

Options

providedIn: Type<any> | 'root' | null
      
      providedIn: Type<any> | 'root' | null
    

Usage notes

Example

@Injectable() class UsefulService { } @Injectable() class NeedsService { constructor(public service: UsefulService) {} } const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]); expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
      
      @Injectable()
class UsefulService {
}

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

const injector = ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService]);
expect(injector.get(NeedsService).service instanceof UsefulService).toBe(true);
    

Injector will throw an error when trying to instantiate a class that does not have @Injectable marker, as shown in the example below.

class UsefulService {} class NeedsService { constructor(public service: UsefulService) {} } expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();
      
      class UsefulService {}

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

expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();