Categories: angularjs

AngularJS Modules Explained

AngularJs Modules.

A module in AngularJS can be thought of as regular packages in Java. It’s the container for the different parts of an application – controllers, services, filters, directives, etc. AngularJS can group together certain functionalities/Javascript under a single module.

  • A module can define it’s own controllers, services, filter, directives, etc which will be accessible throughout the module.
  • A module can depend on other modules.
  • A module can be used by AngularJS to bootstrap an application. By passing the module name to ng-app directive, we can tell AngularJS to load this module as the main entry point for the application.

In angularJS, a module is defined or called using angular.module function.

Define a Module with no dependencies

angular.module('myapp', []);

The first argument is the module name, ‘myApp’. The second argument is an array of module names that this module depends on. Empty square brackets denotes that this module does not have any dependencies.

Define a Module with dependencies

Below snippet defines a module named myApp, which depends on two other modules: ui.bootstrap & ngResource:

angular.module('myapp', ['ui.bootstrap', 'ngResource']);

It means all the functionalities available in ‘ui.bootstrap’ & ‘ngResource’, will be accessible throughout module ‘myApp’.

Load an existing Module

If we just want to load an existing module defined elsewhere, call module function with only one parameter which is name of module.

angular.module('myApp');

Let’s take a simple example:

<html ng-app="myApp">
<head><title>Modle example</title></head>
<body>
 2 + 2 = {{2 + 2}}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js">
</script>
<script>
angular.module('myApp', []);
</script>
</body>
</html>

Above example defines a module. AngularJS then bootstraps the application using this module through the ng-app directive.

Till now, we have just created empty modules. There is nothing inside them. We will add functionality to them in next posts.

Share
Published by

Recent Posts

Spring Boot + AngularJS + Spring Data + JPA CRUD App Example

In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…

7 years ago

Spring Boot Rest API Example

Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…

7 years ago

Spring Boot WAR deployment example

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…

7 years ago

Spring Boot Introduction + hello world example

Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…

7 years ago

Secure Spring REST API using OAuth2

Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…

8 years ago

AngularJS+Spring Security using Basic Authentication

This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…

8 years ago