Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
youdz opened this issue Jul 1, 2017 · 8 comments
Closed

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

youdz opened this issue Jul 1, 2017 · 8 comments
Labels
area: core Issues related to the framework runtime core: di feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Milestone

Comments

@youdz
Copy link
Contributor

youdz commented Jul 1, 2017

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

@vicb vicb added area: core Issues related to the framework runtime feature Issue that requests a new feature labels Jul 5, 2017
@ngbot ngbot bot added this to the Backlog milestone Jan 23, 2018
@darin-cardin
Copy link

Closing in on a year now. Any update on this. it seems like a simple fix, as the functionality already exists.

@agale123
Copy link
Contributor

It appears that this functionality was added as part of #22005 in this commit

So if you want to use the equivalent of @SkipSelf you would write:
injector.get(Foo, undefined, InjectFlags.SkipSelf)

But when I call this from within a Directive and use SkipSelf it is still returning the current instance of the directive.

@chenlijun99
Copy link

Could it be related to #34066?

@mrkeathley
Copy link

This functionality was added to the Injector API but was not implemented in the NodeInjector class which seems like an oversight. I have been looking for an alternative but haven't found a way to use the SkipSelf flag from an autowired injector yet.

@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jun 4, 2021
@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 4, 2021

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 24, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@angular-robot angular-robot bot added the feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors label Jun 24, 2021
@JoostK
Copy link
Member

JoostK commented Oct 6, 2021

Closing as covered by #31776.

@JoostK JoostK closed this as completed Oct 6, 2021
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: di feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature
Projects
None yet
Development

No branches or pull requests

8 participants