Creating AngularJS modules is a simple process that involves calling the angular.module()
method. This method creates an instance of a Module
object, registers it with the injector service, and then returns an instance of the newly created Module
object that you can use to implement provider functionality. The angular.module()
method uses the following syntax:
angular.module(name, [requires], [configFn])
The name
parameter is the name under which the module is registered in the injector service. The requires
parameter is an array of names of modules that are added to the injector service for this module to use. If you need functionality from another module, you need to add it in the requires
list. The ng
module is automatically added to every module instantiated by default, so you have access to the AngularJS providers without explicitly specifying ng
in the list.
Instances of all dependencies are automatically injected into an instance of a module. Dependencies can be modules, services, and any other objects registered in the injector service. The configFn
parameter is another function that is called during the module configuration phase. Configuration functions are described in the next section.
The following is an example of creating an AngularJS module with dependencies on the $window
and $http
services. The definition also includes a configuration function that adds a value
provider named myValue
:
var myModule = angular.module('myModule', ['$window', '$http'], function(){
$provide.value('myValue', 'Some Value'),
});
If you do not specify a requires
parameter, then instead of a Module
object being created, the already-created instance is returned. For example, the following code overwrites the instance defined previously:
var myModule2 = angular.module('myModule', []);
However, the following code returns the instance created previously because no dependencies are listed in the require
array in the parameters list:
var myModule3 = angular.module('myModule'),