WEB TECHNOLOGIES_MODUEL_3(MMC105)DOM and Events
WEB TECHNOLOGIES_MODUEL_3(MMC105)DOM and Events
AngularJS Introduction
What is AngularJs?
AngularJs is an MVVM framework and that help us to create Single Page Applications (SPA) and dynamic web apps.
AngularJS will be maintained and developed by Google Inc.
AngularJs is supported to MVVM and stands for Model-View-Whatever. It's also known as MV*. The (*)
means "whatever you want to do...”.
AngularJS Advantages
If we use angularjs in our applications we have the following advantages.
1. Less Coding
2. More Extensive UI
AngularJS Features
The following are the features of angularjs.
2. MVC pattern
6. Validations
7. Dependency injection
The angularjs uses prefix “ng" for data binding and it handles all the DOM and AJAX requests easily.
3. Scope inheritable
4. DOM manipulations
5. Partial Views
7. Extensible declarative UI
9. Filters Flexibility
Manually we can download AngularJs framework from angularjs site http://angularjs.org/ or we can include following
CDN in header section of our application to use angularjs
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
a. ngApp
b. ng-controller
c. ngModule etc.
AngularJS is a JavaScript framework. It can be added to an HTML page with a <script> tag.
AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.
AngularJS is distributed as a JavaScript file, and can be added to a web page with a script tag:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p ng-bind="name"></p>
</div>
</body>
</html>
Example explained:
The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.
The ng-model directive binds the value of the input field to the application variable name.
The ng-bind directive binds the content of the <p> element to the application variable name.
AngularJS Directives
As you have already seen, AngularJS directives are HTML attributes with an ng prefix.
AngularJS Example
<div ng-app="" ng-init="firstName='John'">
</div>
AngularJS Example
<div data-ng-app="" data-ng-init="firstName='John'">
</div>
You can use data-ng-, instead of ng-, if you want to make your page HTML valid.
You will learn a lot more about directives later in this tutorial.
AngularJS Expressions
AngularJS expressions are written inside double braces: {{ expression }}.
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS expressions bind AngularJS data to HTML the same way as the ng-bind directive.
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>Name: <input type="text" ng-model="name"></p>
<p>{{name}}</p>
</div>
</body>
</html>
AngularJS Applications
AngularJS modules define AngularJS applications.
The ng-app directive defines the application, the ng-controller directive defines the controller.
AngularJS Example
<div ng-app="myApp" ng-controller="myCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>
AngularJS Module
var app = angular.module('myApp', []);
AngularJS Controller
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
AngularJS Expressions
AngularJS binds data to HTML using Expressions.
AngularJS Expressions
AngularJS expressions can be written inside double braces: {{ expression }}.
AngularJS will resolve the expression, and return the result exactly where the expression is written.
AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and
variables.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
If you remove the ng-app directive, HTML will display the expression as it is, without solving it:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div>
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
You can write expressions wherever you like, AngularJS will simply resolve the expression and return the result.
Change the color of the input box below, by changing its value:
lightblue
Example
<div ng-app="" ng-init="myCol='lightblue'">
</div>
AngularJS Numbers
AngularJS numbers are like JavaScript numbers:
Example
<div ng-app="" ng-init="quantity=1;cost=5">
</div>
Example
<div ng-app="" ng-init="quantity=1;cost=5">
</div>
Using ng-init is not very common. You will learn a better way to initialize data in the chapter about controllers.
AngularJS Strings
AngularJS strings are like JavaScript strings:
Example
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
</div>
Example
<div ng-app="" ng-init="firstName='John';lastName='Doe'">
</div>
AngularJS Objects
AngularJS objects are like JavaScript objects:
Example
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
</div>
Example
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
</div>
AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
Example
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
Example
<div ng-app="" ng-init="points=[1,15,19,2,40]">
</div>
AngularJS Modules
An AngularJS module defines an application.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 10 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Creating a Module
A module is created by using the AngularJS function angular.module
<div ng-app="myApp">...</div>
<script>
</script>
The "myApp" parameter refers to an HTML element in which the application will run.
Now you can add controllers, directives, filters, and more, to your AngularJS application.
Adding a Controller
Add a controller to your application, and refer to the controller with the ng-controller directive:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 11 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
Adding a Directive
AngularJS has a set of built-in directives which you can use to add functionality to your application.
In addition you can use the module to add your own directives to your applications:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 12 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script>
app.directive("w3TestDirective", function() {
return {
};
});
</script>
</body>
</html>
In this example, "myApp.js" contains an application module definition, while "myCtrl.js" contains the controller:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 13 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script src="myApp.js"></script>
<script src="myCtrl.js"></script>
</body>
</html>
myApp.js
var app = angular.module("myApp", []);
The [] parameter in the module definition can be used to define dependent modules.
Without the [] parameter, you are not creating a new module, but retrieving an existing one.
myCtrl.js
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName= "Doe";
});
AngularJS Directives
AngularJS directives are extended HTML attributes with the prefix ng-.
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 14 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
</body>
</html>The ng-app directive also tells AngularJS that the <div> element is the "owner" of the AngularJS
application.
Data Binding
The {{ firstName }} expression, in the example above, is an AngularJS data binding expression.
In the next example two text fields are bound together with two ng-model directives:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 15 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<h2>Cost Calculator</h2>
</div>
</body>
</html>
Using ng-init is not very common. You will learn how to initialize data in the chapter about controllers.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 16 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<ul>
{{ x }}
</li>
</ul>
</div>
</body>
</html>
The ng-repeat directive actually clones HTML elements once for each item in a collection.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
<ul>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 17 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</ul>
</div>
</body>
</html>
AngularJS is perfect for database CRUD (Create Read Update Delete) applications.
Just imagine if these objects were records from a database.
The ng-app directive will auto-bootstrap (automatically initialize) the application when a web page is loaded.
Normally, you will not use ng-init. You will use a controller or module instead.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 18 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
To invoke the new directive, make an HTML element with the same tag name as the new directive.
When naming a directive, you must use a camel case name, w3TestDirective, but when invoking it, you must use -
separated name, w3-test-directive:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]">
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 19 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<ul>
</ul>
</div>
</body>
</html>
Element name
Attribute
Class
Comment
Element name
<w3-test-directive></w3-test-directive>
Attribute
<div w3-test-directive></div>
Class
<div class="w3-test-directive"></div>
Comment
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 20 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Restrictions
You can restrict your directives to only be invoked by some of the methods.
Example
By adding a restrict property with the value "A", the directive can only be invoked by attributes:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<w3-test-directive></w3-test-directive>
<div w3-test-directive></div>
<script>
app.directive("w3TestDirective", function() {
return {
restrict : "A",
};
});
</script>
<p><strong>Note:</strong> By setting the <strong>restrict</strong> property to "A", only the HTML element
with the "w3-test-directive" attribute has invoked the directive.</p>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 21 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</body>
</html>
By default the value is EA, meaning that both Element names and attribute names can invoke the directive.
In angularjs ng-model directive is used to get value of input controls like textbox, label, etc and bind that value to application
data. Generally, the syntax of ng-model directive in angularjs like as shown below.
<div ng-app="">
<input type="text" id="txtname" ng-model="name" />
<p>Your Username: {{name}}</p>
</div>
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 22 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
});
</script>
<p>Use the ng-model directive to bind the value of the input field to a property made in the controller.</p>
</body>
</html>
Two-Way Binding
The binding goes both ways. If the user changes the value inside the input field, the AngularJS property will also
change its value:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 23 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
});
</script>
<p>Change the name inside the input field, and you will see the name in the header changes accordingly.</p>
</body>
</html>
Example
<!DOCTYPE html>
<html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 24 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Email:
</form>
<p>Enter your e-mail address in the input field. AngularJS will display an errormessage if the address is not an e-
mail.</p>
</body>
</html>
In the example above, the span will be displayed only if the expression in the ng-show attribute returns true.
If the property in the ng-model attribute does not exist, AngularJS will create one for you.
Application Status
The ng-model directive can provide status for application data (valid, dirty, touched, error):
Example
<!DOCTYPE html>
<html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 25 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Email:
<h1>Status</h1>
</form>
</body>
</html>
OUTPUT:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 26 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
CSS Classes
The ng-model directive provides CSS classes for HTML elements, depending on their status:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<style>
input.ng-invalid {
background-color: lightblue;
</style>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 27 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</form>
<p>Edit the text field and it will get/lose classes according to the status.</p>
<p><b>Note:</b> A text field with the "required" attribute is not valid when it is empty.</p>
</body>
</html>
OUTPUT:
The ng-model directive adds/removes the following classes, according to the status of the form field:
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
In angularjs, the ng-bind directive is used to bind/replace the text content of any particular HTML element with the value of a
given expression that is used against the ng-bind in angularjs application. The value of the specified HTML element will be
updated whenever the value of the given expression will change in angularjs ng-bind directive.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 28 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<div ng-app="">
Enter Name to Display:
<input type="text" ng-model="name">
you entered : <span ng-bind="name"></span>
</div>
Data Model
AngularJS applications usually have a data model. The data model is a collection of data available for the
application.
Example
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
HTML View
The HTML container where the AngularJS application is displayed, is called the view.
The view has access to the model, and there are several ways of displaying model data in the view.
You can use the ng-bind directive, which will bind the innerHTML of the element to the specified model property:
Example
<p ng-bind="firstname"></p>
You can also use double braces {{ }} to display content from the model:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 29 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
<p>First name: {{firstname}}</p>
AngularJS Controllers
AngularJS Controllers
The controllers in angularjs is used to control data of angularjs applications. The AngularJs controller is an object that is
created by using JavaScript constructor.
The angularjs controller will handle DOM elements with the help of using ng-controller directive.
<script type="text/javascript">
var app = angular.module('angularctrlapp', []);
app.controller('angularctrl', function ($scope) {
$scope.msg = 'Welcome to Tutlane.com';
});
</script>
Generally we will define controller with angularjs module. If you observe above code we defined angularjs module with
variable “app” and adding controller to that module. To access controller properties in angularjs application we will use ng-
controller directive. If you observe we are accessing controller properties by defining ng-controller="angularctrl".
Here angularctrl is a JavaScript function.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 30 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
In angularjs controllers are invoked by using angularjs $scope object because of that we initialized $scope object in
controller function.
What is $scope? The $scope in angularjs is an application object. It holds all the variables and functions of controller and
allow us to access those variables and function in application view.
If you observe above syntax we are able to access “msg” variable in view because of defining variable “msg”
with $scope object. We will learn more about $scope in next chapters.
Now we will see how to use controllers in angularjs application with full example.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
AngularJs Controllers Example
</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('angularctrlapp', []);
app.controller('angularctrl', function ($scope) {
$scope.fname = "Welcome to";
$scope.lname = "Tutlane";
});
</script>
</head>
<body>
<div ng-app="angularctrlapp" ng-controller="angularctrl">
First Name: {{fname}}<br />
Last Name: {{lname}}<br />
Full Name: <b>{{fname +" "+ lname}}</b>
</div>
</body>
</html>
If we run above example we will get $scope variables fname and lname values from controller and we can show it in
application.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 31 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
AngularJS Controllers
AngularJS applications are controlled by controllers.
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<br>
</div>
<script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 32 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
</script>
</body>
</html>
Application explained:
The AngularJS application is defined by ng-app="myApp". The application runs inside the <div>.
In AngularJS, $scope is the application object (the owner of application variables and functions).
The controller creates two properties (variables) in the scope (firstName and lastName).
The ng-model directives bind the input fields to the controller properties (firstName and lastName).
Controller Methods
The example above demonstrated a controller object with two properties: lastName and firstName.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 33 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
AngularJS Example
<div ng-app="myApp" ng-controller="personCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
$scope.fullName = function() {
return $scope.firstName + " " + $scope.lastName;
};
});
</script>
Just copy the code between the <script> tags into an external file named personController.js:
AngularJS Example
<div ng-app="myApp" ng-controller="personCtrl">
</div>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 34 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script src="personController.js"></script>
Another Example
For the next example we will create a new controller file:
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<ul>
</li>
</ul>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 35 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script src="namesController.js"></script>
</body>
</html>
AngularJS Scope
The scope is the binding part between the HTML (view) and the JavaScript (controller).
The scope is available for both the view and the controller.
AngularJS Scopes
In angularjs, we have a two main parts one is view (HTML) and another one is controllers. We know that the view represents
whatever (data from data model) the data we want to show it on page and controller will hold all the variables and functions
which we used to show it on page but how they (view and controller) communicate to each other? Well, here scope came
into a picture in angularjs.
Generally, when we create controller in angularjs we will send $scope object as a parameter and set controller properties
(variables and functions) in $scope object. In view we can access those $scope properties and show data that values in page.
So, scope is an object which holds variables and functions in controller and allow us to access that data between Views and
Controller.
In simple words, we can say that scope works as a glue between view (html) and controller and facilitate them to
communicate with each other. The scope values will available in both view and controller.
<script type="text/javascript">
var app = angular.module('angularscopeapp', []);
app.controller('angularctrl', function ($scope) {
$scope.fname = "Welcome to";
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 36 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$scope.lname = "Tutlane";
});
</script>
Once we add properties to $scope object in controller we will get access to use those properties in view (HTML).
In view we can access those $scope properties by defining like {{fname}} we don’t need to prefix with $scope.
We will see how to use $scope in angularjs application with complete example
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
AngularJs Scope Object Example
</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('angularscopeapp', []);
app.controller('angularctrl', function ($scope) {
$scope.fname = "Welcome to";
$scope.lname = "Tutlane";
$scope.getname = function () {
return $scope.fname +" "+ $scope.lname;
};
});
</script>
</head>
<body ng-app="angularscopeapp">
<div ng-controller="angularctrl">
First Name: {{fname}}<br />
Last Name: {{lname}}<br />
Full Name: <b>{{getname()}}</b>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 37 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
</body>
</html>
In above example if you observe we defined variables and functions with $scope object and accessing those values in html.
Now we will run and see the output of above angularjs application.
Example
Properties made in the controller, can be referred to in the view:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{carname}}</h1>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 38 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.carname = "Volvo";
});
</script>
<p>The property "carname" was made in the controller, and can be referred to in the view by using the {{ }}
brackets.</p>
</body>
</html>
When adding properties to the $scope object in the controller, the view (HTML) gets access to these properties.
In the view, you do not use the prefix $scope, you just refer to a property name, like {{carname}}.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 39 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
The scope is a JavaScript object with properties and methods, which are available for both the view and the
controller.
Example
If you make changes in the view, the model and the controller will be updated:
<input ng-model="name">
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
In the two examples above there is only one scope, so knowing your scope is not an issue, but for larger
applications there can be sections in the HTML DOM which can only access certain scopes.
Example
When dealing with the ng-repeat directive, each repetition has access to the current repetition object:
<ul>
<li ng-repeat="x in names">{{x}}</li>
</ul>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 40 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.names = ["Emil", "Tobias", "Linus"];
});
</script>
Each <li> element has access to the current repetition object, in this case a string, which is referred to by using x.
Root Scope
All applications have a $rootScope which is the scope created on the HTML element that contains the ng-
app directive.
If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the
current scope.
Example
A variable named "color" exists in both the controller's scope and in the rootScope:
<body ng-app="myApp">
<div ng-controller="myCtrl">
<p>The scope of the controller's favorite color:</p>
<h1>{{color}}</h1>
</div>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 41 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script>
</script>
</body>
AngularJS Filters
Filters can be added in AngularJS to format data.
AngularJS Filters
In angularjs, filters are used to change the appearance of an expressions by formatting while displaying it to users.
In angularjs, filter is a concept that work with a separation of record based on column and we can apply filters to expressions
and directives by using the pipe (|) character.
{{expression | filter}}
If you observe above syntax, we defined an expression with pipe (I) character, whenever this compiled and executed it will
change the appearance of expression.
Suppose, if we define expression like {{100 | currency}} it will formats the number 100 as currency using currency filter. If
we execute this expression, we will get the result like $100.00.
In angularjs, we can apply filters to result of another filter. Generally, we call it as chaining and the syntax will be like as
shown below.
In angularjs we can format expressions by applying filters with arguments that syntax will be like as shown below.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 42 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
{{expression | filter:argument1:argument2:……}}
Suppose, if we define expression like {{1000 | number:3}} it will format the number 1000 with 3 decimal points using
number expression. If we execute this expression we will get result like 1,000.000.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
AngularJs Filters example
</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<div ng-app="">
Enter Number1:<input type="text" ng-model="number1" /><br /><br />
Enter Number2:<input type="text" ng-model="number2" />
<p>Result of Multiplication: {{number1 * number2 | currency}}</p>
</div>
</body>
</html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 43 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
filter This filter is used to get subset of items from array and returned as array
date This filter is used to convert date to string based on defined format
limitTo This filter is used to create new array or string with specified number of elements
AngularJS Filters
AngularJS provides filters to transform data:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 44 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
$scope.firstName = "John",
$scope.lastName = "Doe"
});
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 45 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</script>
</body>
</html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 46 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('personCtrl', function($scope) {
$scope.firstName = "John",
$scope.lastName = "Doe"
});
</script>
</body>
</html>
Example
The orderBy filter sorts an array:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 47 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 48 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
];
});
</script>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
app.controller('costCtrl', function($scope) {
$scope.price = 58;
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 49 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
});
</script>
</body>
</html>
Read more about the currency filter in our AngularJS currency Filter Reference
The filter filter can only be used on arrays, and it returns an array containing only the matching items.
Example
Return the names that contains the letter "i":
<ul>
<li ng-repeat="x in names | filter : 'i'">
{{ x }}
</li>
</ul>
</div>
Read more about the filter filter in our AngularJS filter Filter Reference
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 50 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Type a letter in the input field, and the list will shrink/grow depending on the match:
Jani
Carl
Margareth
Hege
Joe
Gustav
Birgit
Mary
Kai
Example
<div ng-app="myApp" ng-controller="namesCtrl">
<ul>
<li ng-repeat="x in names | filter : test">
{{ x }}
</li>
</ul>
</div>
Name Country
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 51 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Jani Norway
Carl Sweden
Margareth England
Hege Norway
Joe Denmark
Gustav Sweden
Birgit Denmark
Mary England
Kai Norway
By adding the ng-click directive on the table headers, we can run a function that changes the sorting order of the
array:
Example
<div ng-app="myApp" ng-controller="namesCtrl">
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 52 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
$scope.names = [
{name:'Jani',country:'Norway'},
{name:'Carl',country:'Sweden'},
{name:'Margareth',country:'England'},
{name:'Hege',country:'Norway'},
{name:'Joe',country:'Denmark'},
{name:'Gustav',country:'Sweden'},
{name:'Birgit',country:'Denmark'},
{name:'Mary',country:'England'},
{name:'Kai',country:'Norway'}
];
$scope.orderByMe = function(x) {
$scope.myOrderBy = x;
}
});
</script>
Custom Filters
You can make your own filters by registering a new filter factory function with your module:
Example
Make a custom filter called "myFormat":
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 53 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
{{x | myFormat}}
</li>
</ul>
<script>
</script>
AngularJS Services
In AngularJS you can make your own service, or use one of the many built-in services.
What is a Service?
In AngularJS, a service is a function, or object, that is available for, and limited to, your AngularJS application.
AngularJS has about 30 built-in services. One of them is the $location service.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 54 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
In angularjs, we have a 30 built in services like $http, $location, $timeout, $interval, etc… and these are used to share the
data and its behaviours in the controller, directive, filters and other services over the apps. In angularjs we can create our own
custom services also based on our requirement.
In angularjs, service will create singleton instance over the angular apps and call the services using the service name in the
controller.
The $location service has methods which return information about the location of the current web page:
Example
Use the $location service in a controller:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h3>{{myUrl}}</h3>
</div>
<p>This example uses the built-in $location service to get the absolute url of the page.</p>
<script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 55 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$scope.myUrl = $location.absUrl();
});
</script>
</body>
</html>
OUTPUT:
Note that the $location service is passed in to the controller as an argument. In order to use the service in the
controller, it must be defined as a dependency.
AngularJS constantly supervises your application, and for it to handle changes and events properly, AngularJS
prefers that you use the $location service instead of the window.location object.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 56 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
Use the $http service to request data from the server:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{myWelcome}}</h1>
</div>
<p>The $http service requests a page on the server, and the response is set as the value of the "myWelcome"
variable.</p>
<script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 57 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
</script>
</body>
</html>
This example demonstrates a very simple use of the $http service. Learn more about the $http service in
the AngularJS Http Tutorial.
Example
Display a new message after two seconds:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{myHeader}}</h1>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 58 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
$timeout(function () {
}, 2000);
});
</script>
</body>
</html>
Example
Display the time every second:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 59 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
app.service('hexafy', function() {
this.myFunc = function (x) {
return x.toString(16);
}
});
To use your custom made service, add it as a dependency when defining the controller:
Example
Use the custom made service named hexafy to convert a number into a hexadecimal number:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{hex}}</h1>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 60 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<p>A custom service with a method that converts a given number into a hexadecimal number.</p>
<script>
app.service('hexafy', function() {
return x.toString(16);
});
$scope.hex = hexafy.myFunc(255);
});
</script>
</body>
</html>
To use the service inside a filter, add it as a dependency when defining the filter:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 61 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
app.filter('myFormat',['hexafy', function(hexafy) {
return function(x) {
return hexafy.myFunc(x);
};
}]);
You can use the filter when displaying values from an object, or an array:
<ul>
<li ng-repeat="x in counts">{{x | myFormat}}</li>
</ul>
AngularJS Tables
The ng-repeat directive is perfect for displaying tables.
AngularJS Tables
In angularjs if we want to show data in table formats it’s better to use ng-repeat directive because the table will contain data
in repeated format.
In angularjs the ng-repeat directive will loop through the array data objects in the DOM elements and help us to show data in
tables easily.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 62 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
AngularJS Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 63 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</body>
</html>
OUTPUT:
CSS Style
<!DOCTYPE html>
<html>
<style>
table, th , td {
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 64 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<table>
</tr>
</table>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 65 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
OUTPUT:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 66 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
AngularJS Example
<!DOCTYPE html>
<html>
<style>
table, th , td {
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 67 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 68 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</html>
AngularJS Example
<!DOCTYPE html>
<html>
<style>
table, th , td {
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 69 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<body>
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 70 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</html>
AngularJS Example
<!DOCTYPE html>
<html>
<style>
table, th , td {
border-collapse: collapse;
padding: 5px;
table tr:nth-child(odd) {
background-color: #f1f1f1;
table tr:nth-child(even) {
background-color: #ffffff;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 71 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<body>
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 72 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</body>
</html>
<html>
<style>
table, td {
border-collapse: collapse;
padding: 5px;
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<table>
{{ x.Name }}</td>
<td ng-if="$even">
{{ x.Name }}</td>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 73 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
{{ x.Country }}</td>
<td ng-if="$even">
{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
In angularjs, by using select boxes we can achieve dropdown functionality and we can bind data to dropdown list / select box
using ng-repeat or ng-options directives.
By using ng-model directive in angularjs we can get selected value of select box / dropdown list and we can set the value of
dropdown list and also it will acts as data provider between scope and select box / dropdown list.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 74 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</select>
</div>
<script>
app.controller('myCtrl', function($scope) {
});
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 75 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</script>
<p>This example shows how to fill a dropdown list using the ng-options directive.</p>
</body>
</html>
ng-options vs ng-repeat
You can also use the ng-repeat directive to make the same dropdown list:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<select>
</select>
</div>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 76 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<script>
app.controller('myCtrl', function($scope) {
});
</script>
<p>This example shows how to fill a dropdown list using the ng-repeat directive.</p>
</body>
</html>
Because the ng-repeat directive repeats a block of HTML code for each item in an array, it can be used to create
options in a dropdown list, but the ng-options directive was made especially for filling a dropdown list with
options.
What Do I Use?
You can use both the ng-repeat directive and the ng-options directive:
$scope.cars = [
{model : "Ford Mustang", color : "red"},
{model : "Fiat 500", color : "white"},
{model : "Volvo XC90", color : "black"}
];
Example
Using ng-repeat:
<!DOCTYPE html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 77 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>Select a car:</p>
<select ng-model="selectedCar">
</select>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = [
];
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 78 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
});
</script>
<p>When you use the ng-repeat directive to create dropdown lists, the selected value must be a string.</p>
<p>In this example you will have to choose between the color or the model to be your selected value.</p>
</body>
</html>
Example
Using ng-repeat as an object:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>Select a car:</p>
<select ng-model="selectedCar">
</select>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 79 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = [
];
});
</script>
<p>When you use the ng-repeat directive to create dropdown lists, the selected value must be a string.</p>
<p>In this example you will have to choose between the color or the model to be your selected value.</p>
</body>
</html>
Example
Using ng-options:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 80 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<body>
<p>Select a car:</p>
</select>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = [
];
});
</script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 81 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<p>When you use the ng-options directive to create dropdown lists, the selected value can be an object.</p>
<p>In this example you can display both the model and the color of the selected element.</p>
</body>
</html>When the selected value is an object, it can hold more information, and your application can be more
flexible.
$scope.cars = {
car01 : "Ford",
car02 : "Fiat",
car03 : "Volvo"
};
Example
Using an object as the data source, x represents the key, and y represents the value:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 82 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<p>Select a car:</p>
</select>
</div>
<p>This example demonstrates the use of an object as the data source when creating a dropdown list.</p>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = {
car01 : "Ford",
car02 : "Fiat",
car03 : "Volvo"
});
</script>
</body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 83 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</html>
Example
The selected value will still be the value in a key-value pair, only this time it is an object:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>Select a car:</p>
</select>
<h2>Model: {{selectedCar.model}}</h2>
<h3>Color: {{selectedCar.color}}</h3>
</div>
<script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 84 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
app.controller('myCtrl', function($scope) {
$scope.cars = {
});
</script>
</body>
</html>
The options in the dropdown list does not have to be the key in a key-value pair, it can also be the value, or a
property of the value object:
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p>Select a car:</p>
</select>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 85 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<h2>Model: {{selectedCar.model}}</h2>
<h3>Color: {{selectedCar.color}}</h3>
<p>The visible text inside the dropdown list can also be a property of the value object.</p>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = {
});
</script>
</body>
</html>
AngularJS Forms
Forms in AngularJS provides data-binding and validation of input controls.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 86 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Controls (input, select, textarea) are ways for a user to enter data. A Form is a collection of controls for the purpose of
grouping related controls together.
Form and controls provide validation services, so that the user can be notified of invalid input before submitting a form. This
provides a better user experience than server-side validation alone because the user gets instant feedback on how to correct
the error. Keep in mind that while client-side validation plays an important role in providing good user experience, it can
easily be circumvented and thus can not be trusted. Server-side validation is still necessary for a secure application.
Input Controls
Input controls are the HTML input elements:
input elements
select elements
button elements
textarea elements
Data-Binding
Input controls provides data-binding by using the ng-model directive.
The ng-model directive binds the input controller to the rest of your application.
Example
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 87 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
<form>
</form>
</div>
<script>
app.controller('formCtrl', function($scope) {
$scope.firstname = "John";
});
</script>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<form>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 88 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</form>
</div>
<p>Change the name inside the input field, and you will see the name in the header changes accordingly.</p>
</body>
</html>
Checkbox
A checkbox has the value true or false. Apply the ng-model directive to a checkbox, and use its value in your
application.
Example
Show the header if the checkbox is checked:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<form>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 89 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</form>
</div>
<p>The header's ng-show attribute is set to true when the checkbox is checked.</p>
</body>
</html>
Radiobuttons
Bind radio buttons to your application with the ng-model directive.
Radio buttons with the same ng-model can have different values, but only the selected one will be used.
Example
Display some text, based on the value of the selected radio button:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form>
Pick a topic:
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 90 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
</div>
</div>
<p>The ng-switch directive hides and shows HTML sections depending on the value of the radio buttons.</p>
</body>
</html>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 91 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Selectbox
Bind select boxes to your application with the ng-model directive.
The property defined in the ng-model attribute will have the value of the selected option in the selectbox.
Example
Display some text, based on the value of the selected option:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form>
Select a topic:
<select ng-model="myVar">
<option value="">
<option value="dogs">Dogs
<option value="tuts">Tutorials
<option value="cars">Cars
</select>
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 92 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
</div>
</div>
<p>The ng-switch directive hides and shows HTML sections depending on the value of the dropdown list.</p>
</body>
Last Name:
RESET
form = {"firstName":"John","lastName":"Doe"}
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 93 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
master = {"firstName":"John","lastName":"Doe"}
Application Code
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<form novalidate>
First Name:<br>
Last Name:<br>
<br><br>
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div>
<script>
app.controller('formCtrl', function($scope) {
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 94 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>
</body>
</html>
The novalidate attribute is new in HTML5. It disables any default browser validation.
Example Explained
The ng-app directive defines the AngularJS application.
The ng-model directive binds two input elements to the user object in the model.
The formCtrl controller sets initial values to the master object, and defines the reset() method.
The reset() method sets the user object equal to the master object.
The ng-click directive invokes the reset() method, only if the button is clicked.
The novalidate attribute is not needed for this application, but normally you will use it in AngularJS forms, to
override standard HTML5 validation.
AngularJS Events
AngularJS has its own HTML events directives.
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 95 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
AngularJS Events
You can add AngularJS event listeners to your HTML elements by using one or more of these directives:
ng-blur
ng-change
ng-click
ng-copy
ng-cut
ng-dblclick
ng-focus
ng-keydown
ng-keypress
ng-keyup
ng-mousedown
ng-mouseenter
ng-mouseleave
ng-mousemove
ng-mouseover
ng-mouseup
ng-paste
The event directives allows us to run AngularJS functions at certain user events.
An AngularJS event will not overwrite an HTML event, both events will be executed.
Mouse Events
Mouse events occur when the cursor moves over an element, in this order:
1. ng-mouseover
2. ng-mouseenter
3. ng-mousemove
4. ng-mouseleave
1. ng-mousedown
2. ng-mouseup
3. ng-click
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 96 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
Example
Increase the count variable when the mouse moves over the H1 element:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.count = 0;
});
</script>
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 97 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
</body>
</html>
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 98 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$scope.count = 0;
});
</script>
</body>
</html>
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.count = 0;
Prof.Sachin.G.A,Dept.of.MCA, CIT,Mandya 99 | P a g e
WEB TECHNOLOGIES(Moduel 4 & 5)MMC105
$scope.myFunction = function() {
$scope.count++;
});
</script>
</body>
</html>
Toggle, True/False
If you want to show a section of HTML code when a button is clicked, and hide when the button is clicked again,
like a dropdown menu, make the button behave like a toggle switch:
Click Me
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-show="showMe">
<h1>Menu:</h1>
<div>Pizza</div>
<div>Pasta</div>
<div>Pesce</div>
</div>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.showMe = false;
$scope.myFunc = function() {
$scope.showMe = !$scope.showMe;
});
</script>
</body>
</html>
The myFunc function sets the showMe variable to the opposite of what it is, by using the ! (not) operator.
$event Object
You can pass the $event object as an argument when calling the function.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.myFunc = function(myE) {
$scope.x = myE.clientX;
$scope.y = myE.clientY;
});
</script>
<p>Mouse over the heading to display the value of clientX and clientY from the event object.</p>
</body>
</html>
Form Validation
AngularJS offers client-side form validation.
AngularJS monitors the state of the form and input fields (input, textarea, select), and lets you notify the user
about the current state.
AngularJS also holds information about whether they have been touched, or modified, or not.
You can use standard HTML5 attributes to validate input, or you can make your own validation functions.
Client-side validation cannot alone secure user input. Server side validation is also necessary.
Required
Use the HTML5 attribute required to specify that the input field must be filled out:
Example
The input field is required:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>
</body>
</html>
E-mail
Use the HTML5 type email to specify that the value must be an e-mail:
Example
The input field has to be an e-mail:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form name="myForm">
<input type="email" name="myInput" ng-model="myInput">
</form>
</body>
</html>
They are all properties of the input field, and are either true or false.
They are all properties of the form, and are either true or false.
You can use these states to show meaningful messages to the user. Example, if a field is required, and the user
leaves it blank, you should give the user a warning:
Example
Show an error message if the field has been touched AND is empty:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="">
<form name="myForm">
<p>Name:
</p>
<p>Address:
</p>
</form>
<p>We use the ng-show directive to only show the error message if the field has been touched AND is
empty.</p>
</body>
</html>
CSS Classes
AngularJS adds CSS classes to forms and input fields depending on their states.
The following classes are added to, or removed from, input fields:
Add styles for these classes to give your application a better and more intuitive user interface.
Example
Apply styles, using standard CSS:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<style>
input.ng-invalid {
background-color:pink;
input.ng-valid {
background-color:lightgreen;
</style>
<body ng-app="">
<form name="myForm">
</form>
<p>The input field requires content, and will therefore become green when you write in it.</p>
</body>
Example
Apply styles for unmodified (pristine) forms, and for modified forms:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<style>
form.ng-pristine {
background-color:lightblue;
form.ng-dirty {
background-color:pink;
</style>
<body ng-app="">
<form name="myForm">
<p>The form gets a "ng-dirty" class when the form has been modified, and will therefore turn pink.</p>
</form>
</body>
</html>
Custom Validation
To create your own validation function is a bit more tricky; You have to add a new directive to your application,
and deal with the validation inside a function with certain specified arguments.
Example
Create your own directive, containing a custom validation function, and refer to it by using my-directive.
The field will only be valid if the value contains the character "e":
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body ng-app="myApp">
<form name="myForm">
</form>
<h1>{{myForm.myInput.$valid}}</h1>
<script>
app.directive('myDirective', function() {
return {
require: 'ngModel',
function myValidation(value) {
mCtrl.$setValidity('charE', true);
} else {
mCtrl.$setValidity('charE', false);
return value;
mCtrl.$parsers.push(myValidation);
};
});
</script>
<p>The input field must contain the character "e" to be consider valid.</p>
</body>
</html>
Example Explained:
In HTML, the new directive will be referred to by using the attribute my-directive.
Remember, when naming a directive, you must use a camel case name, myDirective, but when invoking it, you
must use - separated name, my-directive.
Then, return an object where you specify that we require ngModel, which is the ngModelController.
Make a linking function which takes some arguments, where the fourth argument, mCtrl, is the ngModelController,
Then specify a function, in this case named myValidation, which takes one argument, this argument is the value of
the input element.
Test if the value contains the letter "e", and set the validity of the model controller to either true or false.
At last, mCtrl.$parsers.push(myValidation); will add the myValidation function to an array of other functions, which
will be executed every time the input value changes.
Validation Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h2>Validation Example</h2>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<input type="submit"
ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
myForm.email.$dirty && myForm.email.$invalid">
</p>
</form>
<script>
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
$scope.user = 'John Doe';
$scope.email = '[email protected]';
});
</script>
</body>
</html>
The HTML form attribute novalidate is used to disable default browser validation.
Example Explained
The AngularJS directive ng-model binds the input elements to the model.
Because of ng-show, the spans with color:red are displayed only when user or email is $dirty and $invalid.