Injectable

Injectable decorator

What it does

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

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 NoAnnotationError 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();

exported from core-index defined in core/src/di/metadata.ts

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/docs/ts/latest/api/core/index/Injectable-decorator.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部