[英]Injecting directive into controller ends up with “[$injector:unpr] Unknown provider” error
这是我的控制器:
(function () {
'use strict';
angular
.module('app.dashboard', [ 'app.services.dashboardService', 'app.dashboard.eventTableDirective'])
.controller('DashboardController', DashboardController);
DashboardController.$inject = ['dashboardService', '$log', 'eventTableDirective'];
function DashboardController(dashboardService, $log) {
//.....
}
}());
和我的指令:
(function () {
'use strict';
angular
.module('app.dashboard.eventTableDirective', [])
.directive('eventTableDirective', eventTableDirective);
function eventTableDirective() {
var directive = {
link: link,
templateUrl: 'eventTable.directive.html',
restrict: 'EA'
};
return directive;
function link(scope, element, attrs) {
/* */
}
}
}());
虽然完全相同的逻辑可与dashboardService一起使用,但它会因eventTableDirective而失败,并导致此类错误:
错误:[$ injector:unpr]未知提供程序:eventTableDirectiveProvider <-eventTableDirective <-DashboardController
您可以通过在名称后添加“ Directive”后缀来插入指令,因此在您的情况下,它的名称可能会有点儿被称为eventTableDirectiveDirective
:
.controller("FooCtrl", function(eventTableDirectiveDirective){
})
但是, 为什么? 您是否想将指令注入控制器? 也许我缺乏远见,但我无法想象需要这种情况的情况。 指令是显式的View元素-它们在DOM中存在(或消亡)。 控制器不应对View做出任何假设,包括HTML,样式,指令等。
如果我有jQuery背景,建议您阅读“ Thinging in AngularJS”? 了解我在说什么。 实际上,我希望您能读懂它,因为否则我会给您一把枪,您可以用它开枪射击自己的脚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.