使用 $inject 将依赖项注入到控制器中

Dependency injection into controller using $inject

我正在尝试将一些依赖项注入控制器,作为与 John Papa 的风格指南内联的代码重构过程的一部分。现在,我们的控制器看起来像这样:

.controller('alerting-settings.mainController', [
    '$scope',
    '$timeout',
    '$location',    
    '$document',
    '$window',

    function($scope,
             $timeout,
             $location,
             $document,
             $window) {

According to John Papa,更应该这样:

/* recommended */
angular
    .module('app')
    .controller('DashboardController', DashboardController);

DashboardController.$inject = ['$location', '$routeParams', 'common', 'dataservice'];

function DashboardController($location, $routeParams, common, dataservice) {
}

那么当我重构时会发生什么?我最终得到这个:

angular.module('app.alerting-settings')

.controller('alerting-settings.mainController', alerting-settings.mainController);

    alerting-settings.mainController.$inject = [
    '$scope',
    '$timeout',
    '$location',    
    '$document',
    '$window'],

问题是我现在收到控制台错误:

我做错了什么?

您需要在所有地方将 alerting-settings 重命名为 alertingSettings

您可以在文件名中使用“-”,但不能在 controller/service 名称中使用,因为它们是 "functions" 并且在 javascript 中您不能在函数名称中使用破折号

--> function alerting-settings() 不是有效的函数名。