chris Frisina
chris Frisina

Reputation: 19688

Angularjs dependency injection order

I added a filter in a file and now I get the injector error we so all love. I am trying to troubleshoot it, and I think it may have to do with injection order. How do you declare the order that the providers/services/etc must compile before injecting them...

HTML:

<script src="js/app.js"></script>
<script src="js/directives/jokesDirective.js"></script>
<script src="js/filters/jokeFilter.js"></script>
<script src="js/providers/jokeService.js"></script>
<script src="js/controllers/mainController.js"></script>

app.js:

angular.module('jsekoApp', [ 'ui', 'ngRoute', 'ngSlider'])
  .config( ... );

mainController.js:

angular.module('jsekoApp')
  .controller('MainController', [ '$scope', 'JokeFilter', 'JokeService', function MainController($scope, JokeFilter, JokeService) {
    ...
  }]);

The JokeService works fine
The jokeFilter is what is throwing the error - jokeFilter.js:

angular.module('jsekoApp')
  .filter('JokeFilter', [ '$scope', function JokeFilter($scope) {
    ...
  }]);

In the main controller, I declare the filter before the service because the service requires the filter. I assume this is correct. I also declare the filter in the HTML before the service, and after angular and the other similar dependencies..

Full Error:

Error: [$injector:unpr] Unknown provider: JokeFilterProvider <- JokeFilter
http://errors.angularjs.org/1.2.17/$injector/unpr?p0=JokeFilterProvider%20%3C-%20JokeFilter
    at http://127.0.0.1:9000/bower_components/angular/angular.js:78:12
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3735:19
    at Object.getService [as get] (http://127.0.0.1:9000/bower_components/angular/angular.js:3862:39)
    at http://127.0.0.1:9000/bower_components/angular/angular.js:3740:45
    at getService (http://127.0.0.1:9000/bower_components/angular/angular.js:3862:39)
    at invoke (http://127.0.0.1:9000/bower_components/angular/angular.js:3889:13)
    at Object.instantiate (http://127.0.0.1:9000/bower_components/angular/angular.js:3910:23)
    at http://127.0.0.1:9000/bower_components/angular/angular.js:7164:28
    at link (http://127.0.0.1:9000/bower_components/angular-route/angular-route.js:913:26)
    at nodeLinkFn (http://127.0.0.1:9000/bower_components/angular/angular.js:6607:13) <div class="container-fluid ng-scope" ng-view=""> angular.js:9899
(anonymous function) angular.js:9899
(anonymous function) angular.js:7246
nodeLinkFn angular.js:6610
compositeLinkFn angular.js:6011
publicLinkFn angular.js:5916
boundTranscludeFn angular.js:6030
controllersBoundTransclude angular.js:6628
update angular-route.js:871
Scope.$broadcast angular.js:12797
(anonymous function) angular-route.js:552
wrappedCallback angular.js:11408
wrappedCallback angular.js:11408
(anonymous function) angular.js:11494
Scope.$eval angular.js:12518
Scope.$digest angular.js:12330
Scope.$apply angular.js:12622
done angular.js:8234
completeRequest angular.js:8439
xhr.onreadystatechange

Upvotes: 2

Views: 1750

Answers (1)

Matt Way
Matt Way

Reputation: 33161

The problem you have is that you are trying to directly inject a filter. See the solution here: How to use a filter in a controller?

Basically, you can't directly inject the filter itself, and need to use $filter.

You can find more info here: https://docs.angularjs.org/api/ng/service/$filter

Upvotes: 2

Related Questions