2016-12-15 129 views
2

我在我的角度应用程序中使用ng-strict-di模式。它抛出一个错误如何通过以下方式在指令控制器中注入依赖项?

throbberController is not using explicit annotation and cannot be invoked in strict mode

我的代码是:

app.directive('throbberDirective', 
[ 
    '_$ajax', 
    function(_$ajax){ 
     return { 
      restrict: "EA", 
      templateUrl: "common/utils/throbbers/throbber.html", 
      controller: throbberController 
     } 
     function throbberController($scope){ 
      $scope.throbber = _$ajax.getThrobberConfigs(); 
      $scope.throbber.templateName = $scope.throbber.templateName; 

     } 
     throbberController.$inject = ['$scope']; 
    } 
]); 

如何明确地注入?我做错了什么?帮我解决这个问题。

+0

尝试将指令和它的控制器分隔成两个单独的文件。然后你可以在控制器中像通常那样进行注射。 – rrd

+0

为什么它不在这里发生? – SaiUnique

回答

2
app.directive('throbberDirective', 
[ 
    function(){ 
     return { 
      restrict: "EA", 
      templateUrl: "common/utils/throbbers/throbber.html", 
      controller: throbberController 
     } 
    } 
]); 
app.controller('throbberController', throbberController); 
throbberController.$inject = ['$scope', '_$ajax']; 
function throbberController($scope){ 
    $scope.throbber = _$ajax.getThrobberConfigs(); 
    $scope.throbber.templateName = $scope.throbber.templateName; 

} 
+1

这工作正常。谢谢。 – SaiUnique

+0

为什么我以前的过程失败了? – SaiUnique

+1

'throbberController。$ inject = ['$ scope'];'在此行中'throbberController未定义' –