Self

decorator stable

npm Package @angular/core
Module import { Self } from '@angular/core';
Source core/src/di/metadata.ts

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

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v4.angular.io/api/core/Self