Angular directive throws Error: [$injector:unpr]

旧巷老猫 提交于 2019-12-25 16:51:52

问题


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.


回答1:


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
    };
  }]);
})();



回答2:


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
        };
    }

})();


来源:https://stackoverflow.com/questions/43114108/angular-directive-throws-error-injectorunpr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!