指令中的角度控制器导致严格的 di 错误 [英] Angular controller in directive cause strict di error

查看:30
本文介绍了指令中的角度控制器导致严格的 di 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了 ng-strict-di mod.它适用于我所有的 DI,除非我尝试在指令中使用控制器.仅供参考,我使用 John Papa 风格指南来格式化我的代码.

I use the ng-strict-di mod in my app. It works for all my DI, except when I try to use a controller in a directive. FYI I use the John Papa style guide to format my code.

这是我的指令:

(function () {
    'use strict';

    angular
        .module('app.mymodule')
        .directive('myDirective', myDirective);

    myDirective.$inject = [];

    function  myDirective() {
        var directive = {
            bindToController: true,
            controller: MyDirectiveCtrl,
            controllerAs: 'vm',
            restrict: 'E',
            scope: {
                data:'='
            },   
            templateUrl: 'my-directive-template.html',  
        };
        return directive;

        MyDirectiveCtrl.$inject = ['ServiceSvc'];
        function MyDirectiveCtrl(ServiceSvc) {
            var vm = this;

            vm.foo = foo;

            function foo() {
                ServiceSvc.bar();
            }
        }
    }
})();

所以我在我的 MyDirectiveCtrl 中明确地注入了我的 ServiceSvc 但在我的 Chrom 控制台中我有一个关于严格 DI 的错误:

So I inject explicitly my ServiceSvc in my MyDirectiveCtrl but in my Chrom console I've got an error about strict DI :

Error: [$injector:strictdi] MyDirectiveCtrl is not using explicit annotation and cannot be invoked in strict mode
http://errors.angularjs.org/1.4.3/$injector/strictdi?p0=MyDirectiveCtrl 
    at vendor.js:10
    at Function.annotate [as $$annotate] (vendor.js:209)
    at Object.invoke (vendor.js:233)
    at extend.instance (vendor.js:443)
    at nodeLinkFn (vendor.js:374)
    at vendor.js:397
    at processQueue (vendor.js:703)
    at vendor.js:704
    at Scope.$eval (vendor.js:748)
    at Scope.$digest (vendor.js:743)

知道为什么我会收到这个错误吗?

Any idea why i get this error ?

推荐答案

我认为你必须在你的指令之外使用你定义的控制器,如下

I think you have to use the controller defined in your directive outside, as below

 angular
        .module('app.mymodule')
        .directive('myDirective', myDirective);

myDirective.$inject = [];

function  kissNotificationList() {
    var directive = {
        bindToController: true,
        controller: MyDirectiveCtrl,
        controllerAs: 'vm',
        restrict: 'E',
        scope: {
            data:'='
        },   
        templateUrl: 'my-directive-template.html',  
    };
    return directive;


}

//direcitves controller

 MyDirectiveCtrl.$inject = ['ServiceSvc'];

 function MyDirectiveCtrl(ServiceSvc) {
        var vm = this;

        vm.foo = foo;

        function foo() {
            ServiceSvc.bar();
        }
    }

这篇关于指令中的角度控制器导致严格的 di 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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