Skip to content Skip to sidebar Skip to footer

Why Is My Directive Throwing "error: $injector:unpr Unknown Provider"

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

Solution 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 */functiondirective () {
        var directive = {
            templateUrl : "header/platform_header/platformHeader.html",
            restrict    : "E",
            replace     : true,
            bindToController: true,
            controller: Controller,
            controllerAs: 'vm',
            link: link,
            scope: {
            }
        };
        return directive;
        functionlink(scope, element, attrs) {

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

    }
})();

Solution 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).

Post a Comment for "Why Is My Directive Throwing "error: $injector:unpr Unknown Provider""