Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - when defining a module, what is the meaning of third argument when it is an array?

Tags:

angularjs

I am new to AngularJS. I have been reading the excellent book Mastering Web Application Development with AngularJS by Pawel Kozlowski and Peter Bacon Darwin. However, I am still a bit fuzzy on some concepts, so I have decided to go through their sample application line by line to try and get a better understanding of how AngularJS is used in a real-world app.

In some places I see notation that I can't see explained in their book, nor in the API docs. I am wondering if anyone can shed some light on this, as seen in the /client/src/app/projectsinfo/projectsinfo.js file of the project linked to above:

angular.module('projectsinfo', [], ['$routeProvider', function($routeProvider){ 
    ...
}]);

My understanding of the angular.module method is that it accepts three arguments:

  • the name of the module
  • an array of other modules that this module might depend on
  • an optional configuration function

However, in the above example, for the third argument an array is being provided, with the first element in the array being a string (I am assuming a provider?), followed by a function. Can anyone explain what is going on here?

like image 991
Jeff Fohl Avatar asked Nov 22 '13 02:11

Jeff Fohl


People also ask

How define module in AngularJS?

var app = angular. module("myApp", []); The [] parameter in the module definition can be used to define dependent modules.

What is the meaning of this Angular module?

In Angular, a module is a mechanism to group components, directives, pipes and services that are related, in such a way that can be combined with other modules to create an application. An Angular application can be thought of as a puzzle where each piece (or each module) is needed to be able to see the full picture.

Which function is used on module to define values in AngularJS?

javascript - Angular. js: Is . value() the proper way to set app wide constant and how to retrieve it in a controller - Stack Overflow.

Which function is used to create a module in AngularJS?

module() method creates an application module, where the first parameter is a module name which is same as specified by ng-app directive.


1 Answers

The syntax of angular.module() is:

angular.module(name, [requires], configFn);

where:

  • name is the name of the module,
  • requires is an optional list of modules on which this module depends, and
  • configFn is a function used to configure the module.

Here the configFn can be a function or a array:

  • If it is a function, then the dependencies injected to it will be injected based on the name of the parameters.
  • If it is an array, then we can use the array to specify the name of the services that need to be injected and then use some other name as the function parameter. This is useful when your code might be obfuscated by a minifier.

The code in the said files seems to be fine.

like image 194
Arun P Johny Avatar answered Sep 21 '22 04:09

Arun P Johny