Add ability to use inject() for element injector hierarchy
See original GitHub issueWhich @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
Please provide the environment you discovered this bug in (run ng version
)
Angular 14.2.3
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:9 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 getrunInContext
added to theInjector
class?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.