这两种配置 $routeProvider 的方式有什么区别?

What is the difference between these 2 ways to config the $routeProvider?

我正在学习 Angular 并且我看到了一些示例,其中 $routeProvider 的配置如下:

app.config(function ($routeProvider) {
        $routeProvider.
            when("/drivers", { templateUrl: "partials/drivers.html", controller: "driversController" }).
            when("/drivers/:id", { templateUrl: "partials/driver.html", controller: "driverController" }).
            otherwise({ redirectTo: "/drivers" });
    });

在其他示例中,它是这样配置的:

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when("/drivers", { templateUrl: "partials/drivers.html", controller: "driversController" }).
        when("/drivers/:id", { templateUrl: "partials/driver.html", controller: "driverController" }).
        otherwise({ redirectTo: "/drivers" });
}]);

它们似乎都以相同的方式工作,但我想知道通过首先发送一个带有 '$routeProvider' 作为字符串的数组然后调用函数来调用配置之间的区别是什么就像第一个例子一样直接使用函数。

谢谢。

This is not specific to $routeProvider, it's every module you can declare dependencies for.

您看到的是三种不同的依赖注入方式中的两种(内联数组和隐式,另一种是 $inject 属性),数组注入更好,因为缩小会导致问题不在数组语法中:

app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider.
        when("/drivers", { templateUrl: "partials/drivers.html", controller: "driversController" }).
        when("/drivers/:id", { templateUrl: "partials/driver.html", controller: "driverController" }).
        otherwise({ redirectTo: "/drivers" });
}]);

将服务名称(要注入的)包装在方括号中,然后是函数本身,将使它的缩小变得安全。这是执行此操作的内联注释方式。