本文介绍了指令中的角度控制器导致严格的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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!