1

We are running a hybrid upgrade from angular js to angular 10.
I have a service that I upgraded to angular. Then I downgraded it to inject into an existing angular JS service (we have quite a few, so we need to do piecemeal).

I found this: https://angular.io/guide/upgrade#upgrading-the-phone-service

However, this deals with injecting into a component, not an angular service. Long story short, I get the "unknown provider" error.

I need help downgrading services and injecting them into existing angular services.

Thanks

limbo93
  • 6,488
  • 1
  • 12
  • 14

1 Answers1

0

How we've done it:

  1. Create a service in Angular x, decorate it with @Injectable (we used @Injectable({ providedIn: "root" }));
  2. Have a specialised AngularJS module (we call it the downgraded.module) and have the following code:
import { downgradeInjectable } from "@angular/upgrade/static";

angular
  .module("downgraded.module", [])
  .factory("MyService", downgradeInjectable(MyService));
  1. Register that module anywhere you want in your AngularJS environment with downgraded.module as a dependency
angular
  .module('app', ['downgraded.module']);
  1. Just inject the service like any other.
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • I have very similar to this pattern but still ended up with 'Error: Trying to get the Angular injector before bootstrapping the corresponding Angular module.' Any suggestions please – CrazyMac Jan 31 '21 at 13:36
  • Please ask a new question with a MRE of your setup, it will make it a lot easier to answer – Ruben Helsloot Jan 31 '21 at 22:02
  • 1
    @CrazyMac - Check the following which shows how to use a service bootstrap component to jumpstart Angular loading when the 1st migrated artifacts are services like in your example; it will fix the error you're mentioning: https://stackoverflow.com/a/48668591/410937 – atconway Oct 19 '21 at 22:46