Saturday, October 8, 2016

AngularJS - Modules

Servers as a container for the different parts of an application like controllers, service, directive, filters, etc.  Example: A controller always belong to a module.

Module defines an application. You can think Module as the Main method in other types of applications, which is the starting point of an application. It specifies how angular framework will bootstrap the application. A module can depend on other modules. Once the module is created you can add controllers, directives, filters and more to it. Modules are defined inside the javascript file. 

Creating Module: 

Use module method of angular object to create the module.
            var myApp = angular.module("myApp", []);
  • The first parameter is the name of the module.
  • The [] parameter in the module definition can be used to define dependent modules.
  • Without the [] parameter, you are not creating a new module, but retrieving an existing one.

There is only one ng-app attribute being used in one of the html controls, in an AngularJS application that defines the starting point of the angularJS application. Hence, one of the module name should match with the value of the attribute ng-app.

Advantage of using Module:
Global functions in javascript can easily be overwritten or destroyed by other scripts. AngularJS modules reduces this problem, by keeping all the functions local to the module.

No comments:

Post a Comment