Error: [$injector:unpr] Unknown provider – while migrating AngularJS1 to Angular6

I’m trying to do upgrade components written in AngularJS1 to Angular6. I’m taking the approach of having the wrappers for all the existing AngularJS1 component by extending “UpgradeComponent” placed under the folder “directive-wrappers” in my example.

while loading the application I get the console error as

Error: [$injector:unpr] Unknown provider: testDirective2DirectiveProvider <- testDirective2Directive
https://errors.angularjs.org/1.7.8/$injector/unpr?p0=testDirective2DirectiveProvider%20%3C-%20testDirective2Directive
    at eval (angular.js:138)
    at eval (angular.js:4924)
    at Object.getService [as get] (angular.js:5084)
    at eval (angular.js:4929)
    at Object.getService [as get] (angular.js:5084)
    at Function.UpgradeHelper.getDirective (upgrade_helper.ts:56)
    at new UpgradeHelper (upgrade_helper.ts:52)
    at TestDirective2Wrapper.UpgradeComponent (upgrade_component.ts:106)
    at new TestDirective2Wrapper (TestDirective2Wrapper.ts:27)
    at createClass (provider.ts:265) "<app-root _nghost-c0="">"

enter image description here

To mimic the same application online I have created the same angularjs directives and its wrappers in stackblitz

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

There’s an issue with the docsSampleDirective.js file.

You’ve initialized the AngularJS Module there for the second time when you already initialized the testApp module in the app.module.js file. Just get rid of the dependency array from there:

angular
  .module("testApp")
  .controller("Controller", [
    "$scope",
    function($scope) {
      $scope.customer = {
        name: "Naomi",
        address: "1600 Amphitheatre"
      };
    }
  ])
  .directive("docsSimpleDirective", function() {
    return {
      template: "Name: {{customer.name}} Address: {{customer.address}}"
    };
  });

Here’s a Working Sample StackBlitz for your ref.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply