An Phan An Phan - 1 year ago 215
Javascript Question

AngularJS Error: Unknown provider: aProvider <- a

OK I'm officially bald now, after having been streching my hair out with this infamous problem: The minfied AngularJS app just doesn't work, with this error thown out:

Error: [$injector:unpr] Unknown provider: aProvider <- a$injector/unpr?p0=aProvider%20%3C-%20a
at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11492
at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26946
at Object.c [as get] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250)
at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:27041
at c (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26250)
at Object.d [as invoke] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:26496)
at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:910
at Object.f [as forEach] (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:4:11927)
at http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:9:856
at j (http://localhost/my-app/dist/scripts/1bde0e2e.vendor.js:5:27235)

Lots of other people had this problem as well, but looks like it could be fixed by declaring dependencies as an array instead of bare function parameters, like this:

angular.module('my-app').controller('LoginCtrl', [ '$scope', 'HttpService', function($scope, HttpService) { ... }]);

instead of this:

angular.module('my-app').controller('LoginCtrl', function($scope, HttpService) { ... });

But it doesn't work in my case. I checked all of my scripts (coffee and generated javascripts as well), they all use the proper array-style declaration.

The problem doesn't come from extra packages apparently. I tried moving all extra package references out of
<!-- bower:js -->
block (so that they are not minified by grunt), yet the problem still remains. Which means, it's my code to blame... But then again, I've tried the (seems to be) only fix available, to no avail.

Any hint, even on how to properly debug this?

Thanks in advance!

Answer Source

Finally I've found the problem. And yes, it's a DI bug which I missed.

To all who may be suffering from the same headache: array-format declaration must be done in $routeProvider's resolve options as well. In my case (CoffeeScript ahead):

app.config (['$routeProvider', ($routeProvider) ->
    .when '/',
      templateUrl: 'views/main.html'
      controller: 'MainCtrl'
        groups: ['GroupService', (GroupService) ->  # I MISSED THIS
          return GroupService.getAll()
        entries: ['EntryService', (EntryService) ->  # AND THIS
          return EntryService.getAll()
    # ...

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download