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

时间秒杀一切 提交于 2019-12-12 06:47:33

问题


I just saw an example of routing in AngularJS. I would like to know the relation ship between dependency 'ngRoute' and module mainApp, in synatx var mainApp = angular.module("mainApp", ['ngRoute']);.

Previously I've seen examples with empty square brackets in module declaration.

Below is the whole context of code:

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'
           });
     }]);

回答1:


[...] defines an array

In the angular case.

mainApp is a main module( main array) and ngRoute is a sub module(like array of object).

The sample is

var ngRoute=[];//{}

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



回答2:


In angular, when defining a module(creating it), you pass it the names of other modules it depends on as an array (in square brackets).

In your example, the mainApp-module depends on the ngRoute-module, making the components of ngRoute(directives, services, factories, values...) available for dependency injection for the components in mainApp. To define a module that does not depend on any other modules, you pass an empty array ([]) See the angular documentation for some more info on modules



来源:https://stackoverflow.com/questions/31989675/what-is-the-meaning-of-the-square-brackets-in-modulemodulename-of-an

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!