为什么我的指令抛出“错误:$ injector:unpr未知提供程序"? [英] Why is my Directive throwing "Error: $injector:unpr Unknown Provider"

查看:79
本文介绍了为什么我的指令抛出“错误:$ injector:unpr未知提供程序"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将控制器,工厂和指令重构为推荐的用于 Angular代码段的"Angular-Style-Guide " .

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

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

具有错误的新指令样式:

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

    }
})();

我的旧版正常运行指令中不会抛出错误:

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

            }]
        }
    });

})();

推荐答案

$scope不能注入指令.我已更改代码以将$scope注入指令的控制器中.

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

代码:

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

    }
})();

这篇关于为什么我的指令抛出“错误:$ injector:unpr未知提供程序"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆