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.

Add ability to use inject() for element injector hierarchy

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When providing a Service in a component, inject() triggers NullInjectorError when called within runInContext :

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [Service],
})
export class AppComponent {
  public constructor(injector: EnvironmentInjector, service: Service) {
    console.log(service); // OK
    console.log(inject(Service)); //OK

    setTimeout(() => {
      injector.runInContext(() => {
        console.log(inject(Service)); //KO
      });
    }, 100);
  }
}

This does not happen

  • if the service is providedIn:'root'
  • if the service is provided in the module

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-yccqgi?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

Please provide the environment you discovered this bug in (run ng version)

Angular 14.2.3

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
antischematiccommented, Sep 30, 2022

It’s possible to create an environment injector that inherits from the node injector, would it be possible to widen the type signature so that createEnvironmentInjector allows this without a type cast? Alternatively, could we get runInContext added to the Injector class?

@Component({
  providers: [Service]
})
class AppComponent {
  constructor(parent: Injector) {
    const injector= createEnvironmentInjector([], parent as EnvironmentInjector)
    setTimeout(() => {
      injector.runInContext(() => {
        console.log(inject(Service)); // now it works
      });
    }, 100);
  }
}
1reaction
JeanMechecommented, Sep 28, 2022

Thx @pkozlowski-opensource, It this mentioned somewhere in the documentation ? If not, it would be a good idea to add it.

In my use case, I have objects created outside DI, but they still need to get access to injected services.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hierarchical injectors - Angular
Hierarchical dependency injection enables you to share dependencies between different parts of the application only when and if you need to. Types of...
Read more >
Understand Angulars Hierarchical Dependency Injection system
A hierarchical dependency injection system allows us to define different boundaries or scopes for our dependencies to run in and follows the ...
Read more >
Dependency Injection in Angular - JavaScript in Plain English
Angular DI makes use of a hierarchical injection system, due to this nested injectors are able to create their own service instances.
Read more >
Hierarchical Dependency Injectors - Angular
When you specify providers in the @Injectable() decorator of the service itself (typically at the app root level), optimization tools such as those...
Read more >
How to @Inject into existing object hierarchy using Guice?
What I would suggest is this basic pattern: make a visitor that traverses the hierarchy and, as it goes, it does injection on...
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