SkipSelf annotation to get parent component Injector no longer working in Ivy (Angular 9 rc.9)
See original GitHub issue🐞 bug report
Affected Package
The issue is caused by package @angular/core
Is this a regression?
Yes, with Angular 8 this was still possible.
Description
I have a component B that is a child of component A. With previous Angular versions I was able to inject the Injector
of component A into component B by simply using the @SkipSelf
annotation on the injected service. With that, I could then resolve services in the context of the parent component.
In my use case, both A and B provide the same injection token with different fixed values and component B needs to know what this value was for its parent component.
🔬 Minimal Reproduction
https://stackblitz.com/edit/angular-parent-injector-gx5pha
The example on StackBlitz is of course in Angular 8 (since I can’t seem to do Angular 9 rc projects) and works as expected. The output on the console is:
this is "HELLO COMPONENT" and parent has name "THIS IS ROOT"
this is "HELLO COMPONENT" and parent has name "THIS IS ROOT"
When I download the example and upgrade it to Angular 9 rc.9 I get different output:
this is "HELLO COMPONENT" and parent has name "THIS IS ROOT"
this is "HELLO COMPONENT" and parent has name "HELLO COMPONENT"
The second line is produced with the parent Injector
injection approach.
🌍 Your Environment
Angular Version: I tested this with Angular 9 rc.7, rc.8 and rc.9 and the example fails in all of them.
Anything else relevant?
In the past I have tried other aproaches to solve this issue, one of them was to use the Injector.get
method and specifying the InjectFlags
argument as documented in the Injector. It seems that in all versions of Angular (that I tested), the injector instance that I get when requesting an Injector
on a component simply ignores the inject flags - in the source code I can see that the method simply does not use this argument. Somehow I find this very weird since the documentation does not seem to mention that.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (9 by maintainers)
Hi @mehmet-erim, while working on this problem we came across several cases that would require additional refactoring to support them. We also found some inconsistencies with the way these cases are handled in View Engine, so we’ll need a bit more time to come up with proper solution for Ivy. We’ll share more updates once we have them. Thank you.
@elca-lan looks like a bug to me 👍