Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

            }]
        }
    });

})();
like image 565
Leon Gaban Avatar asked Dec 19 '22 04:12

Leon Gaban


1 Answers

$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) {

    }
})();
like image 60
jad-panda Avatar answered Jan 05 '23 09:01

jad-panda