Injectable
Marks a class as available to Injector
for creation.
Option | Description |
---|---|
providedIn
|
Determines which injectors will provide the injectable,
by either associating it with an @NgModule or other |
See also
Usage notes
The following example shows how service classes are properly marked as injectable.
@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
throws an error if it tries to instantiate a class that
is not decorated with @Injectable
, as shown in the following example.
class UsefulService {}
class NeedsService {
constructor(public service: UsefulService) {}
}
expect(() => ReflectiveInjector.resolveAndCreate([NeedsService, UsefulService])).toThrow();