How to create module in angularjs and What is the use of module in angularjs?

How to create module in angularjs and What is the use of module in angularjs?

According to Angular Doc - “The angular.module is a global place for creating, registering and retrieving AngularJs modules. All modules (AngularJs core or 3rd party) that should be available to an application must be registered using this mechanism”.

“Passing one argument retrieves an existing angular. module, whereas passing more than one argument creates a new angular.module”.

Syntax of AngularJs Module looks like:

var app = angular.module("MyApp", []);
//The above (with [] module) is indicate that this setter function it’s used for register a module.

var app = angular.module("MyApp");
//The above (without [] module) is indicate that this is getter function and it’s used for retrieve a previously registered module.

The AngularJs module is a container for applications and the controllers always belong to a module. Also, an AngularJs module defines an application.

Methods of Angular Modules:-
1.      info([info])
2.      provider(name, providerType)
3.      factory(name, providerFunction)
4.      service(name, constructor)
5.      value(name, object)
6.      constant(name, object)
7.      decorator(name, decorFn)
8.      animation(name, animationFactory)
9.      filter(name, filterFactory)
10. controller(name, constructor)
11. directive(name, directiveFactory)
12. component(name, options)
13. config(configFn)
14. run(initializationFn)

The Examples are,
var app = angular.module("MyApp", []).controller("contr1", function () { }); // module MyApp has controller contr1.

var app = angular.module("MyApp").controller("contr2", function () { }); // module MyApp has controllers contr1, contr2.

var app = angular.module("MyApp", []); // module MyApp  registered to this module.
app.controller("contr3", function () { }); // module MyApp has controller contr3 only.

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2 Powered by Blogger.