Which means [] in angular.module

Asked

Viewed 382 times

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

From what I understand, the parameter myApp is the name of the module, but not in the official documentation I was able to understand the meaning of the empty list in the second parameter. What is this list for?

2 answers

6


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

Translated into Portuguese from documentation:

Modules can list other modules as their dependencies. Depending on a module implies that the necessary module needs to be loaded before the required module is loaded. By others words, the configuration blocks of the necessary modules are executed before the applicant module configuration blocks. The same is true for the execution blocks. Each module can only be once loaded, 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 dependency:

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

6

It is the dependency set of this module that is being created.

Say you create your module myapp and need to use the standard Angular route mechanism.

Then, it will be necessary to install the module (download it and refer it to the project) and "inject it" into its module, in this way

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

And this is necessary for any and all modules that are used within myapp.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.