Useful or not, from you.
angular Error: inject() must be called from an injection context

<!-- PLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.

ISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION. -->

I'm submitting a...

<!-- Check one of the following options with "x" --> <pre><code> [X] Regression (a behavior that used to work and stopped working in a new release) [ ] Bug report <!-- Please search GitHub for a similar issue or PR before submitting --> [ ] 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: </code></pre>

Current behavior

<!-- Describe how the issue manifests. --> 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: 2018-09-05 14_52_21-

Expected behavior

<!-- Describe what the desired behavior would be. --> 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?

<!-- Describe the motivation or the concrete use case. --> 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

<pre><code> Angular version: 6.1.6 <!-- Check whether this is still an issue in the most recent Angular version -->

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: <!-- Anything else relevant? Operating system version, IDE, package manager, HTTP server, ... --> </code></pre>

That's a useful answer
Without any help

I have the same problem, my workspace includes 2 angular applications (version 8.0) and an angular library. I installed the library in the applications by 'npm link' command. I also added preserveSymlinks: true to the angular.json of both applications. Now, one application works perfect and the other doesn't and below error is shown: image I just tried to inject a service from the library! How can i fix it? is it something related to new angular ivy?

update:

  1. I found if i inject a simple service from library then application works perfect, but if i inject a service which another service injected to it, then above error emerges.

  2. using npm install instead of npm link lead to error too.

  3. both applications updated from angular v7 to angular v8.