Please refer to the following article for the basics:

In this blog we will see,

  • What is module and what is a controller
  • How to create a module and controller
  • How it works

What is Module and how to create it?

  1. Module is a kind of Main () method in AngularJS which contains controllers, services, directives etc.
  2. Module in AngularJS can be created by using following syntax.
  3. Module is used with ng-app.

A Module can be dependent on another module like ngRoute, ui.bootstrap.

What is a controller and how to create a controller?

  1. Controller is a function which defines the data for view to display.
  2. It can retrieve data from the database by calling a service. Controller is used with ng-controller.
  3. The syntax to create controller is,

$scope is an object to which the data is attached and it is then retrieved and displayed.

Here message is attached to $scope object. So in the Html page which is using this controller by using double curly braces {{message}} the message will be displayed.

The controller needs to be registered with a module like this



Complete Code for Creating Module, Controller and registering Controller with Module is,

  1. varmyApp = angular.module("myModule", [])
  2. varmyController = function ($scope) {
  3. $scope.message = "Welcome to angular JS";
  4. };
  5. myApp.controller("myController", myController);
or
  1. varmyApp = angular
  2. .module("myModule", [])
  3. .controller("myController", function ($scope) {
  4. $scope.message = "Welcome to angular JS";
  5. });
Steps to Create Application,

Step 1

Download Angular.JS file from AngularJS.org.

Step 2

Create a website in Visual Studio. Add Angular.js file in it.

Find the below screenshots for reference.




  • Now add one html page and just drag and drop angular.min.js in your HTML page. It will look like this.
    1. <head>
    2. <scriptsrc="Scripts/angular.js"></script>
    3. </head>
  • Now add one Javascript file and just drag and drop angular.js in your JS file. It will look like this.
    1. /// <reference path="angular.js" />
  • Following is the code for the Javascript file .
    1. varmyApp = angular.module("myModule", [])
    2. .controller("myController", function($scope)
    3. {
    4. var employee = {
    5. firstName: "rekha",
    6. lastName: "Kulkarni",
    7. gender: "f→emale",
    8. };
    9. var country = {
    10. Name: "India",
    11. Capital: "delhi",
    12. };
    13. // Attaching to $scope Object
    14. $scope.message = "Welcome to angular JS";
    15. $scope.country = country;
    16. $scope.employee = employee;
    17. });
  • Following is the code for html file.
    1. <!DOCTYPEhtml>
    2. <html xmlns="http://www.w3.org/1999/xhtml">
    3. <head>
    4. <script src="Scripts/angular.js"></script>
    5. <script src="Scripts/JavaScript.js"></script>
    6. </head>
    7. <body ng-app="myModule">
    8. <div ng-controller="myController">
    9. {{message}}
    10. <br/>
    11. <br/>
    12. </div>
    13. <hr/>
    14. <div ng-controller="myController">
    15. <div>
    16. Country Name: {{country.Name}}
    17. <br/>
    18. <br/> Country Capital: {{country.Capital}}
    19. </div>
    20. <hr/>
    21. <div>
    22. Employee FirstName :{{employee.firstName}}
    23. <br/> Employee LastName :{{employee.lastName}}
    24. <br/> Gender :{{employee.gender}}
    25. </div>
    26. </div>
    27. </body>
    28. </html>
  • The result of the program is: