Click here to Skip to main content
15,889,863 members
Articles / AngularJs

AngularJS: Controllers: Tutorial 2

Rate me:
Please Sign up or sign in to vote.
3.00/5 (3 votes)
25 Dec 2015CPOL1 min read 8.6K   3  
AngularJS: Controllers: Tutorial 2

Article Series

  1. Tutorial 1: Angular JS
  2. Tutorial 2: Controllers
  3. Tutorial 3: Controllers
  4. Tutorial 4: Controllers : Advanced
  5. Tutorial 5: Models
  6. Tutorial 6: Services

In this tutorial, we will be learning about Controllers in Angular.

Controllers are used to bind a view to its business logic. Ideally, a view should have a controller. All the initialization stuff for that particular view also goes into the controller, e.g., Greeting a user while he logs in, etc.

In brief:

  1. Initialize the view using $scope object.
  2. Add business logic to a view

For any other purpose apart from these, one needs to create angular services.

We will go ahead and look at how to define controllers:

  1. Using Scope object:
    CSS
    .controller('ControllerName1', function($scope)
                          {
                              $scope.name = "Aditya";
                          });
  2. Plain function:
    CSS
    .controller('ControllerName', function()
                          {
                              var scope = this;
                              scope.name = "Aditya S";
                          });

Now how do we use this in an HTML page. We need to first give our HTML application an ng-app namespace as:

HTML
<html lang="en" ng-app="Namespace">

Then, we can put the above scripts as below:

HTML
<head>
<meta charset="utf-8">
<title>Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js">
 </script>
<script>
 var Namespace = angular.module('Namespace',[]);
 
 Namespace.controller('ControllerName', function()
 {
 var scope = this;
 scope.name = "Aditya S";
 });
 
 Namespace.controller('ControllerName1', function($scope)
 {
 $scope.name = "Aditya";
 });
 </script>
</head>

We can go ahead and use them in our body as:

HTML
<body>
<div ng-controller="ControllerName as control">Hello {{control.name}}</div>
<div ng-controller="ControllerName1">Hello {{name}}</div>
</body>

From the above, we are binding both the Controllers in a different manner, as the first one creates a local variable which can be used by “classname.propertyname” and the second uses scope to create and hence can be directly used with “propertyname”.

The entire application looks like:

HTML
<!DOCTYPE html>
<html lang="en" ng-app="Namespace">
<head>
<meta charset="utf-8">
<title>Hello World</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js">
 </script>
<script>
 var Namespace = angular.module('Namespace',[]);
 
 Namespace.controller('ControllerName', function()
 {
 var scope = this;
 scope.name = "Aditya S";
 });
 
 Namespace.controller('ControllerName1', function($scope)
 {
 $scope.name = "Aditya";
 });
 </script>
</head>
 
<body >
<div ng-controller="ControllerName as control">Hello {{control.name}}</div>
<div ng-controller="ControllerName1">Hello {{name}}</div>
</body>
</html>

Happy Angularing!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Passionate about Microsoft Technologies like WPF, Windows Azure, ASP.NET, Win Phone and also on Cross platform Mobile Apps, Mongo DB's, IOT & WOT. I love learning and working with (or) Creating Design Patterns . Writer | Technology Evangelist | Technology Lover | Microsoft Developer Guidance Advisory Council Member | Cisco Champion | Speaker |

Blog : http://adityaswami89.wordpress.com/

Comments and Discussions

 
-- There are no messages in this forum --