Angular modules and controllers

In this session we will learn

  • What is a module and how to create a module
  • What is a controller and how to create a controller
  • How to register a controller with the module
  • How to use the module that we created to bootstrap the angular application.

What is the module?
A module is a container for different parts of your application i.e controllers, services, directives,filters, etc. We will learn about module and controller in this session.

why is the module required?
You can think of a module as a Main() method in other types of applications.

How to create a module?
Use the angular objects module() method to create a module.

var myApp = angular.module("myModule",[]);

first paramter specifies the name of the module
second parameter specifies the dependencies of the module, a module can depend on other modules.

What is a controller?
in angular a controller is a Javascript function, Thejob of the constructor is to build a model for the view to display, model is nothing but data, in real world application controller mimic the web service which is used to retrieve the data from the database.
We can pass data from one page to any page with in the navigation happens through controller and services.

How to create a controller in agular?
We will create a anonymous function as shown below and assign it a variable.
var myController = function ($scope) {
$scope.message = "Angular JS Tutorial.";
}

here, $scope is angular object, passed by the angular framework automatically
We will attach the model to the scope object. like we attached the message in the above object.
Use the data binding expression in the view to display the data attached already to model object
Lets summarize everything in the fig below.

Leave a Reply

Your email address will not be published. Required fields are marked *