Injectable
decorator
stable
npm Package | @angular/core |
---|---|
Module | import { Injectable } from '@angular/core'; |
Source | core/src/di/metadata.ts |
How To Use
@Injectable() class Car {}
Description
For more details, see the "Dependency Injection Guide".
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);
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();
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v5.angular.io/api/core/Injectable