Why is my Directive throwing “Error: $injector:unpr Unknown Provider”

微笑、不失礼 提交于 2019-12-10 15:15:11

问题


I've working on refactoring my Controllers, Factories and Directives to the recommended Angular-Style-Guide for Angular Snippets.

I've gotten the Controllers and Factories working correctly with the new style, but not the Directives.

Unknown provider: $scopeProvider <- $scope <- platformHeaderDirective

The new Directive style with errors:

(function() { "use strict";

    angular
        .module('platformHeaderDirectives', [])
        .directive('platformHeader', directive);
    directive.$inject = ['$scope'];
    /* @ngInject */
    function directive ($scope) {
        var directive = {
            templateUrl : "header/platform_header/platformHeader.html",
            restrict    : "E",
            replace     : true,
            bindToController: true,
            controller: Controller,
            controllerAs: 'vm',
            link: link,
            scope: {
            }
        };
        return directive;
        function link(scope, element, attrs) {

        }
    }
    /* @ngInject */
    function Controller () {

    }
})();

My old working Directive that does not throw errors:

(function() { "use strict";

    angular.module('platformHeaderDirectives', [])

    .directive('platformHeader', function() {
        return {
            templateUrl : "header/platform_header/platformHeader.html",
            restrict    : "E",
            replace     : true,
            scope       : false,
            controller  : ['$scope',
                           function($scope) {

                /** Init platformHeader scope */
                // var vs = $scope;

            }]
        }
    });

})();

回答1:


$scope can not be injected to directive. i have changed code to inject $scope in controller of directive.

Code:

(function() { "use strict";

    angular
        .module('platformHeaderDirectives', [])
        .directive('platformHeader', directive);
    
    /* @ngInject */
    function directive () {
        var directive = {
            templateUrl : "header/platform_header/platformHeader.html",
            restrict    : "E",
            replace     : true,
            bindToController: true,
            controller: Controller,
            controllerAs: 'vm',
            link: link,
            scope: {
            }
        };
        return directive;
        function link(scope, element, attrs) {

        }
    }
    /* @ngInject */
    Controller.$inject = ['$scope'];
    function Controller ($scope) {

    }
})();



回答2:


I know you got your answer but let me explain the actual picture.

$scope is not a service($scopeProvider is not exist in angular js) it is something special that is injected by angular itself into the controller as a child of $rootScope.

so you cannot explicitly inject it in service,directive...etc.

But as the answer explained by 'jad-panda' you can inject it explicitly in the controller of direcitve (not directly to the directive).



来源:https://stackoverflow.com/questions/30271903/why-is-my-directive-throwing-error-injectorunpr-unknown-provider

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