W3cubDocs

/Angular 2

Self decorator

What it does

Specifies that an Injector should retrieve a dependency only from itself.

How to use

@Injectable() class Car { constructor(@Self() public engine:Engine) {} }

Description

For more details, see the Dependency Injection Guide.

Example

class Dependency {}

@Injectable()
class NeedsDependency {
  constructor(@Self() public dependency: Dependency) {}
}

let inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
const nd = inj.get(NeedsDependency);

expect(nd.dependency instanceof Dependency).toBe(true);

inj = ReflectiveInjector.resolveAndCreate([Dependency]);
const child = inj.resolveAndCreateChild([NeedsDependency]);
expect(() => child.get(NeedsDependency)).toThrowError();

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

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