这是我的控制器:
(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
发布于 2015-07-21 17:37:19
您可以通过在名称中添加一个后缀“指令”来注入指令,因此在您的示例中,它将不幸地命名为eventTableDirectiveDirective。
.controller("FooCtrl", function(eventTableDirectiveDirective){
})但是,为什么?!,您想要向控制器注入指令吗?也许我缺乏远见,但我无法想象需要这样做的情景。指令是显式查看元素-它们在DOM中存在(和死亡)。控制器不应对视图做出任何假设,包括HTML、样式、指令等。
我建议你读“Thinking in AngularJS” if I have a jQuery background?来理解我说的话的要旨。事实上,我希望你读过它,因为否则我给了你一把枪,你会用它射中自己的脚。
https://stackoverflow.com/questions/31544854
复制相似问题