Error: inject() must be called from an injection context
See original GitHub issueI’m submitting a…
[X] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[ ] Performance issue
[ ] 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
[ ] Other... Please describe:
Current behavior
While creating a library I noticed that if you consume the library and try to inject a service that in turn has a dependency on either NgModuleRef
, Injector
or ApplicationModule
it throws an error as shown below:
Expected behavior
In Angular 5 we injected NgModuleRef
without any problems, in Angular 6 one can still inject these classes, just not from anything that’s in a library.
in other words, in Angular 5 this code worked fine, but breaks in Angular 6:
public constructor(private readonly appModule: NgModuleRef<any>) {
//
}
Minimal reproduction of the problem with instructions
I’ve created a Github repository showcasing the issue here
What is the motivation / use case for changing the behavior?
We inject NgModuleRef
in our company’s internal libraries for on the fly reconfiguration of services, for this we need an injector that can resolve those services.
If there’s a better way of accessing the injector from a service (in a library, from application itself it works!), that’s a solution for me too.
Environment
Angular version: 6.1.6
Browser:
- [X] Chrome (desktop) version 68.0.3440.106
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [X] Firefox version 61.0.2
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [X] Edge version 42.17134.1.0
For Tooling issues:
- Node version: 9.11.1
- Platform: Windows 10
Others:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:36 (5 by maintainers)
Top GitHub Comments
in angular.json of the project calling the angular lib via the link
projects.$name.architect.build.options.preserveSymlinks: true
note: make sure to set preserveSymlinks to true, fixed our issue