Skip to content

DI: provide an equivalent to @SkipSelf in Injector.get() #17898

Description

@youdz

I'm submitting a ...


[ ] Regression (behavior that used to work and stopped working in a new release)
[ ] Bug report 
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

It is currently possible, when injecting a dependency in a constructor, to use @Optional() to avoid an error when the dependency is not found, and @SkipSelf() to search directly on the parent injector. For instance, a typical use case for @SkipSelf() is self-nested components that need to inject their parent.

export class MyComponent {
  constructor(@Optional() @SkipSelf() parent: MyComponent) {}
}

When working with more "dynamic" dependency injection, however, you need to rely on an Injector object. It offers an equivalent to @Optional() by accepting a notFoundValue, but there is no way to emulate @SkipSelf(). The parent injector is not public, and the only method is .get() which will obviously look in this injector first.

Expected behavior

The get() method should accept a third optional argument allowing us to search directly on the parent injector:

abstract get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T, skipSelf?: boolean): T;

It would default to false, evidently.

What is the motivation / use case for changing the behavior?

It should be logical for Injector to offer the exact same capabilities as constructor-level dependency injection.

Please tell us about your environment


Angular version: 4.2.5

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: difeatureLabel used to distinguish feature request from other issues

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions