AngularJS Controller

In AngularJS, a controller is a javaScript constructor function which is attached with HTML DOM using ng-controller attribute to defines a controller to be bound with the view. In AngularJS, controller provides great control over view and model in order to fatch the data as per request and display in the view. Most important thing here is your model is lives inside the controller and therefore accept $scope as a parameter.

Once you decleare ng-controller to the parent element, all child elements automatically belongs to that controller. You can think AngularJS controller as regular Javascript object with properties and functions. A controller is defined using ng-controller directive in AngularJS.

AngularJS controller used to:

  • To intialized state of the $scope object.
  • To add behavior to the $scope object.

Different ways to define controller in AngularJS

Syntax1 : Define AngularJS Controller

function CustomerController($scope) {
  $scope.customer = { name: "Jimi Scott" };
}

Example1

<html>
<head>
<title>My first AngularJS Controller</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
<Script>
//Defining AngularJS Controller Using Standard Javascrip Object Constructor
function CustomerController($scope) {
  $scope.customer = { name: "Jimi Scott", address: "12-13-283/A1", email: "Jimi@yahoo.com" };
}
</Script>
</head>
<body>
<div ng-app="">
<div ng-controller="CustomerController"> 
<p>Customer Name : {{ customer.name }}</p>
<p>Customer Name : {{ customer.address }}</p>
<p>Customer Name : {{ customer.email }}</p>

</div>
</div>
</body>
</html>
Try Now

Syntax2 : Define AngularJS Controller

var app = angular.module('myApp', []);
    app.controller('CustomerController', function($scope) {
      $scope.customer = { name: "Jimi Scott" };
    });

Example2

<html>
<head>
<title>My first AngularJS code</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
<Script>
//Defining AngularJS Controller Using AngularJS Module
var app = angular.module('myApp', []);
   app.controller('CustomerController', function($scope) {
   $scope.customer = { name: "Jimi Scott", address: "12-13-283/A1", email: "Jimi@yahoo.com" };
    });
</Script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="CustomerController"> 
<p>Customer Name : {{ customer.name }}</p>
<p>Customer Name : {{ customer.address }}</p>
<p>Customer Name : {{ customer.email }}</p>

</div>
</div>
</body>
</html>
Try Now

AngularJS Controller with Action

AngularJS allow you to add action handler method in controler and call these method from view. You just need to call the method as per require on the controller.

Syntax

<button id="btnSubmit" ng-click="onBtnClick()">Click Here</button>

Example3

<html ng-app>
<head>
<title>My first AngularJS code</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
<Script type="text/javascript">
//Creating AngularJS controller here
function validateForm($scope)  
{  
   $scope.show = function() {
	if($scope.firstName == undefined || $scope.firstName == '')
        alert('Name is Required'); 
	else
		alert('Hi - ' + $scope.firstName);
    };  
} 
</Script>
</head>
<body>
<div ng-controller="validateForm" >
    <form name="newForm">
        Enter Name: <input type="text" id="firstName" name="firstName" 
        ng-model="firstName" ng-maxlength="20"> </br>
		<button ng-click="show()" >Submit</button>
    </form> 
</div>
</body>
</html>
Try Now

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives


 
 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.