Babulaas
Babulaas

Reputation: 869

Angular directive throws Error: [$injector:unpr]

I am working on an project and need to modify some code to resolve an error
directive:

(function () {
    'use strict';

    angular.module('myApp').directive("myDirective", function ($filter) {
        return {
            // code
        };
    });
})();

This throws an error with the minified version only. I am using angular 1.5.9

I think I need to define $filter somewhere.

Upvotes: 1

Views: 675

Answers (2)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41417

When you are using minified version of angular you need to inject the dependencies as a separate string array. Otherwise, dependency injector unable to identify which is which

(function () {
    'use strict';

    angular.module('myApp')
      .directive("myDirective",myDirective);

    myDirective.$inject = ['$filter'];

    function myDirective($filter) {
        return {
            // code
        };
    }

})();

Upvotes: 1

rrd
rrd

Reputation: 5957

I assume you have the app already defined somewhere.

You seem to have not injected $filter, try this instead:

(function () {
  'use strict';

  angular.module('myApp').directive("myDirective", ["$filter", function ($filter) {
    return {
      // code
    };
  }]);
})();

Upvotes: 4

Related Questions