AngularJS的“.module("modulename", […])”中的方括号是什么意思

What is the meaning of the square brackets in “.module("modulename", […])” of AngularJS

我刚刚在 AngularJS 中看到了一个 example 路由。我想知道在 synatx var mainApp = angular.module("mainApp", ['ngRoute']);.

中依赖 'ngRoute' 和模块 mainApp 之间的关系

以前我见过模块声明中带有空方括号的示例。

下面是整个代码上下文:

var mainApp = angular.module("mainApp", ['ngRoute']);

  mainApp.config(['$routeProvider',
     function($routeProvider) {
        $routeProvider.
           when('/addStudent', {
              templateUrl: 'addStudent.htm',
              controller: 'AddStudentController'
           }).
           when('/viewStudents', {
              templateUrl: 'viewStudents.htm',
              controller: 'ViewStudentsController'
           }).
           otherwise({
              redirectTo: '/addStudent'
           });
     }]);

在 angular 中,定义 module(创建它)时,将它所依赖的其他 module 的名称作为数组(在方括号中)传递给它。

在您的示例中,mainApp-module 依赖于 ngRoute-module,使 ngRoute 的组件(指令、服务、 factories, values...) 可用于 mainApp 中组件的依赖注入。要定义不依赖于任何其他模块的 module,您传递一个空数组 ([]) See the angular documentation for some more info on modules

[...] 定义一个数组

在angular的情况下。

mainApp 是主模块(主数组),ngRoute 是子模块(如对象数组)。

样本是

var ngRoute=[];//{}

var mainApp=[ngRoute];// now the `mainApp` includes the `ngRoute`