如何以下列方式在指令控制器中注入依赖项?

时间:2016-12-15 12:56:15

标签: angularjs dependency-injection angular-directive

我在我的角应用中使用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'];
    }
]);

如何明确注射?我做错了吗?帮我解决这个问题。

1 个答案:

答案 0 :(得分: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;

}