question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
AndrewKushnircommented, Apr 3, 2020

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.

2reactions
JoostKcommented, Jan 16, 2020

@elca-lan looks like a bug to me 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

SkipSelf - Angular
Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the parent injector. Resolution works...
Read more >
A Deep Dive into @Injectable and providedIn in Ivy - Angular ...
In this article I want to explain how Angular @Injectable decorator works under the hood and how providedIn property is processed by Angular...
Read more >
angular/core - UNPKG
23, throw Error('Could not find renamed property on target object.'); ... This should only happen if we are running ivy outside Angular and....
Read more >
angular - Root Provider for Recursive @SkipSelf Components
The solution to this ended up being improper use of the providers property in the @Component annotation. It should only be present in...
Read more >
fesm2015/core.js - Bundle Scanner
ɵcmpp){if(o=jo(r,e),-1==o)throw new Error("The provided component was not found in the ... createElementNS(n,t)}function gi(e,t){const n=e[9],r=n.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found