What does [] mean in angular.module

6

I'm a beginner in AngularJS and started reading about modules in an application. When I want to create a new module, I write the following line of code:

var myAppModule = angular.module('myApp', []);

As far as I understand, the myApp parameter is the name of the module, but even in official documentation I can not understand meaning of the empty list in the second parameter. What is this list for?

    
asked by anonymous 31.01.2017 / 17:43

2 answers

6

The second parameter is the dependency list of the module being created.

Translated to Portuguese from documentation :

  

Modules can list other modules as their dependencies.   Depending on a module it implies that the required module needs to be   loaded before the required module is loaded. By others   words, the configuration blocks of the required modules are   executed before the requesting module configuration blocks. O   same is true for the execution blocks. Each module can only be   loaded once, even if several other modules require it.

Example loading a configuration service

angular.module('configService', []).value('parametros', {
    URI_PRODUCER: "http://meu_ip:8080/"
});

Another module that loads the above service as a dependency:

var app = angular.module('segundoModulo', ['configService']);
app.controller('meuController', function(parametros){
  console.log(parametros.URI_PRODUCER) // irá exibir http://meu_ip:8080
})
    
31.01.2017 / 17:50
6

It is the dependency set of this module being created.

Let's say you create your myApp module and need to use the default route mechanism of the Angular.

Then you will need to install the module (download it and reference it in the project) and "inject it" in your module, this way

var myAppModule = angular.module('myApp', ['ngRoute']);

And this is required for every module that is used within myApp .

    
31.01.2017 / 17:51