Module 4& 5 Angular Js
Module 4& 5 Angular Js
MODULE 4 & 5
Syllabus:
1.1AngularJS Introduction
AngularJS simplifies web development by allowing developers to extend HTML with new
attributes and bind data easily to the HTML elements.
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>
Advantages of AngularJS:
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 1
Web Technologies 22MCA24
2.0 Directives
AngularJS lets you extend HTML with new attributes called Directives.
AngularJS has a set of built-in directives which offers functionality to your applications.
AngularJS also lets you define your own directives.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 3
Web Technologies 22MCA24
Description: The ng-if directive conditionally includes or removes elements from the
DOM based on the value of an expression. If the expression evaluates to true, the element
is added to the DOM; if false, the element is removed.
Purpose: Used to conditionally display or hide elements.
Example:
<p ng-if="isVisible">This text is shown only if isVisible is true</p>
g. ng-show and ng-hide (Visibility Control)
Description:
o ng-show: Displays an element if the expression evaluates to true (using display:
block).
o ng-hide: Hides an element if the expression evaluates to true (using display:
none).
Purpose: Used to toggle visibility without removing the element from the DOM.
Example:
<p ng-show="isVisible">This text is visible</p>
<p ng-hide="isHidden">This text is hidden</p>
h. ng-class (Dynamic Class Binding)
Description: The ng-class directive dynamically binds CSS classes to an HTML element
based on the value of an expression. It can add or remove classes dynamically based on
the data in the scope.
Purpose: Apply different styles to elements based on the model data.
Example:
<p ng-class="{active: isActive, disabled: !isActive}">This element has dynamic classes</p>
i. ng-style (Dynamic Styling)
Description: The ng-style directive dynamically sets CSS styles on an HTML element
based on an expression. You can apply multiple styles dynamically using an object.
Purpose: Dynamically apply CSS styles to elements.
Example:
<p ng-style="{'color': myColor, 'font-size': mySize}">This text has dynamic styling</p>
j. ng-click (Event Handling)
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 4
Web Technologies 22MCA24
Description: The ng-click directive attaches a click event handler to an HTML element.
When the element is clicked, the expression provided is evaluated.
Purpose: Handle click events in AngularJS applications.
Example:
<button ng-click="sayHello()">Click Me</button>
k. ng-view (Single Page Application Routing)
Description: The ng-view directive is used in Single Page Applications (SPAs) to load
and inject different views dynamically into the same page based on the current route. It
works in conjunction with AngularJS’s routing mechanism.
Purpose: Used to load partial templates and controllers dynamically based on the URL.
Example:
<div ng-view></div>
2. Custom Directives
Custom directives in AngularJS allow you to create reusable components that can encapsulate
specific behaviors or functionality. Custom directives can be used to define your own HTML
tags, attributes, or class-based directives that encapsulate and modularize the behavior.
Creating a Custom Directive:
Custom directives are defined using the .directive() method of an AngularJS module.
Basic Structure:
angular.module('myApp', [])
.directive('myDirective', function() {
return {
restrict: 'E', // 'E' for Element, 'A' for Attribute
template: '<h1>Custom Directive Example</h1>'
};
});
Usage Example:
<my-directive></my-directive>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 5
Web Technologies 22MCA24
template or templateUrl: Defines the HTML content or external HTML file to be used
by the directive.
scope: Specifies an isolated or shared scope for the directive.
3.0 AngularJS Expressions
AngularJS binds data to HTML using Expressions.
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.
<!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>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 6
Web Technologies 22MCA24
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 7
Web Technologies 22MCA24
<body>
<div ng-app="" ng-init="person={firstName:'John',lastName:'Doe'}">
<p>The name is {{ person.lastName }}</p>
</div>
</body>
</html>
3.4 AngularJS Arrays
AngularJS arrays are like JavaScript arrays:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="" ng-init="points=[1,15,19,2,40]">
<p>The third result is {{ points[2] }}</p>
</div>
</body>
</html>
AngularJS Expressions vs. JavaScript Expressions
Like JavaScript expressions, AngularJS expressions can contain literals, operators, and variables.
Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.
AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript
expressions do.
AngularJS expressions support filters, while JavaScript expressions do not.
4.0 AngularJS Controllers
AngularJS controllers control the data of AngularJS applications.
AngularJS controllers are regular JavaScript Objects.
AngularJS applications are controlled by controllers.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 8
Web Technologies 22MCA24
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "John";
$scope.lastName = "Doe";
});
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 9
Web Technologies 22MCA24
</script>
</body>
</html>
Application explained:
The AngularJS application is defined by ng-app="myApp". The application runs inside the
<div>.
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller.
The myCtrl function is a JavaScript function.
AngularJS will invoke the controller with a $scope object.
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).
5.0 Filters
AngularJS filters are used to format data before it is displayed to the user. They can be applied in
expressions, directives, or services to transform data. Filters allow you to format text, date,
currency, and more, without changing the underlying data in your application.
2. Number Filters:
o number: Formats a number as text using a local-specific format (e.g., adding
commas or decimal points).
o currency: Formats a number as a currency value, based on the locale and
currency symbol.
o date: Formats a date object or timestamp into a specified date format.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 10
Web Technologies 22MCA24
3. Array Filters:
o filter: Filters an array based on a criteria or function.
4. Custom Filters:
o AngularJS allows you to create custom filters for specific data transformations
that are not covered by built-in filters.
Using Filters
1. Text Filters
uppercase and lowercase:
<p>{{ 'hello world' | uppercase }}</p> <!-- Output: HELLO WORLD -->
<p>{{ 'HELLO WORLD' | lowercase }}</p> <!-- Output: hello world -->
limitTo:
<p>{{ 'AngularJS Filters are powerful' | limitTo: 7 }}</p> <!-- Output: Angular -->
2. Number Filters
number:
<p>{{ 1234567.89 | number:2 }}</p> <!-- Output: 1,234,567.89 -->
currency:
<p>{{ 1234.56 | currency:'€' }}</p> <!-- Output: €1,234.56 -->
date:
<p>{{ today | date:'fullDate' }}</p> <!-- Output: e.g., Wednesday, September 25, 2024 -->
3. Array Filters
filter:
<!-- Assuming `students` is an array of objects with a `name` property -->
<p ng-repeat="student in students | filter:searchText">{{ student.name }}</p>
<input type="text" ng-model="searchText" placeholder="Search students">
orderBy:
<ul>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 11
Web Technologies 22MCA24
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 12
Web Technologies 22MCA24
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 13
Web Technologies 22MCA24
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<p>The url of this page is:</p>
<h3>{{myUrl}}</h3>
</div>
<p>This example uses the built-in $location service to get the absolute url of the page.</p>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $location) {
$scope.myUrl = $location.absUrl();
});
</script>
</body>
</html>
2.The $http Service
The $http service is one of the most common used services in AngularJS applications. The
service makes a request to the server, and lets your application handle the response.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 15
Web Technologies 22MCA24
<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>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("welcome.htm").then(function (response) {
$scope.myWelcome = response.data;
});
});
</script>
</body>
</html>
3.The $timeout Service
The $timeout service is AngularJS' version of the window.setTimeout function.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 16
Web Technologies 22MCA24
<h1>{{myHeader}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $timeout) {
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
}, 2000);
});
</script>
</body>
</html>
4.The $interval Service
The $interval service is AngularJS' version of the window.setInterval function.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 17
Web Technologies 22MCA24
<h1>{{theTime}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $interval) {
$scope.theTime = new Date().toLocaleTimeString();
$interval(function () {
$scope.theTime = new Date().toLocaleTimeString();
}, 1000);
});
</script>
</body>
</html>
Create Your Own Service
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 18
Web Technologies 22MCA24
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:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<h1>{{hex}}</h1>
</div>
<p>A custom service with a method that converts a given number into a hexadecimal
number.</p>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 19
Web Technologies 22MCA24
<script>
app.service('hexafy', function() {
return x.toString(16);
});
$scope.hex = hexafy.myFunc(255);
});
</script>
</body>
</html>
An Events in AngularJS can be used to perform particular tasks, based on the action taken. Both
Angular Event & the HTML Event will be executed & will not overwrite with an HTML Event.
It can be added using the Directives mentioned below:
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 20
Web Technologies 22MCA24
1. ng-mousemove: The movement of the mouse leads to the execution of the event.
2. ng-mouseup: Movement of the mouse upwards leads to the execution of the event.
3. ng-mousedown: Movement of the mouse downwards leads to the execution of the event.
4. ng-mouseenter: Click of the mouse button leads to the execution of the event.
9. ng-keyup: Press of upward arrow key leads to the execution of the event.
10. ng-keydown: Press of downward arrow key leads to the execution of the event.
16. ng-paste: It is used to specify custom behavior functions when the text in input fields is
pasted into an HTML element.
17. ng-mouseleave: It is used to apply custom behavior when a mouse-leave event occurs on a
specific HTML element.
<!DOCTYPE html>
<html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 21
Web Technologies 22MCA24
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<p>
</p>
<div ng-app="App1"
ng-controller="Ctrl1">
NCEH
</h1>
<h2>Total Count:</h2>
</div>
<script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 22
Web Technologies 22MCA24
app.controller('Ctrl1', function($scope) {
$scope.count = 0;
});
</script>
</body>
</html>
Example:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script></
head><body ng-controller="myCtrl">
<p>{{message}}</p>
<script>
app.controller('myCtrl', function($scope) {
$scope.message = "";
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 23
Web Technologies 22MCA24
$scope.showMessage = function() {
};
});
</script>
</body></html>
Example:
<script>
app.controller('myCtrl', function($scope) {
$scope.resetMessage = function() {
};
});</script>
Example:
<script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 24
Web Technologies 22MCA24
app.controller('myCtrl', function($scope) {
$scope.key = "";
$scope.showKey = function(event) {
};
});</script>
Example:
<form ng-submit="submitForm()">
<button type="submit">Submit</button></form><p>{{name}}</p>
<script>
app.controller('myCtrl', function($scope) {
$scope.submitForm = function() {
};
});</script>
The ng-mouseover directive triggers an action when the mouse pointer hovers over an element.
Example:
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 25
Web Technologies 22MCA24
<script>
app.controller('myCtrl', function($scope) {
$scope.hoverMessage = function() {
};
});</script>
Ex0ample:
<script>
app.controller('myCtrl', function($scope) {
$scope.message = "";
$scope.onFocus = function() {
};
$scope.onBlur = function() {
};
});</script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 26
Web Technologies 22MCA24
$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) {
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 27
Web Technologies 22MCA24
$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>
Forms are collection of controls that is input field, buttons, checkbox and these can
be validatedreal time. As soon as a user of the form completes writing a field and moves to
the next one itgets validated and suggests the user where he might have went wrong.
Input Controls
1. input elements
2. select elements
3. button elements
4. textarea elements
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 28
Web Technologies 22MCA24
Data-Binding
The ng-model directive binds the input controller to the rest of your application.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<form>
</form>
</div>
<p>Change the name inside the input field, and you will see the name in the header changes
accordingly.</p>
</body>
</html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 29
Web Technologies 22MCA24
1. Checkbox
A checkbox has the value true or false. Apply the ng-model directive to a checkbox, and use its
value in your application.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="">
<form>
</form>
</div>
<p>The header's ng-show attribute is set to true when the checkbox is checked.</p>
</body>
</html>
2. Radiobuttons
Radio buttons with the same ng-model can have different values, but only the selected one will
be used.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 30
Web Technologies 22MCA24
<!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:
</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">
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 31
Web Technologies 22MCA24
<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>
3. Selectbox
The property defined in the ng-model attribute will have the value of the selected option in the
selectbox.
<!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
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 32
Web Technologies 22MCA24
<option value="tuts">Tutorials
<option value="cars">Cars
</select>
</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
dropdown list.</p>
</body>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 33
Web Technologies 22MCA24
</html>
AngularJS provides a comprehensive set of built-in tools to handle form validation, allowing you
to easily manage the state of form inputs, display error messages, and ensure data integrity
before submission. It tracks form and field validity through different states and offers validation
feedback through model properties such as $valid, $invalid, $touched, $dirty, etc.
1. Form Model and Control States: AngularJS forms track the state of form controls and
provide properties like $valid, $invalid, $dirty, $touched, and $untouched to handle form
validation.
2. Built-In Validators: AngularJS provides several built-in validation directives, such as:
3. Custom Validation: You can also create custom validation rules using directives and
$validators.
$touched: True if the form/field has been focused and then blurred.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 34
Web Technologies 22MCA24
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="myCtrl">
<div>
<label>Username:</label>
</div>
<div>
<label>Email:</label>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 35
Web Technologies 22MCA24
</div>
<div>
<label>Password:</label>
</div>
</form>
<p>{{message}}</p>
<script>
app.controller('myCtrl', function($scope) {
$scope.submitForm = function(isValid) {
if (isValid) {
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 36
Web Technologies 22MCA24
} else {
};
});
</script>
</body>
</html>
The form is created using the <form> tag, and the name="userForm" attribute assigns it a
reference.
Each input field (username, email, and password) is bound to a model using ng-model.
Validation is added to these fields using built-in directives like ng-required, ng-minlength, and
the HTML type="email" for email validation.
Validation Feedback:
Error messages are displayed using ng-show when certain conditions are met, such as when the
field is invalid (userForm.username.$error.required) and has been interacted with
(userForm.username.$touched).
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 37
Web Technologies 22MCA24
For example, if the username field is empty and the user has clicked and then clicked away from
it, the error message "Username is required!" will be displayed.
ng-minlength / ng-maxlength: Ensures that the input length is within a defined range.
Email Validation: Using the HTML5 type="email" ensures the input is in valid email format.
You can create custom validation rules using AngularJS directives. Custom validators are added
using $validators in a directive.
<form name="userForm">
<label>Password:</label>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 38
Web Technologies 22MCA24
<span ng-show="userForm.password.
$error.specialChar">Password must contain a special
character!</span>
</form>
<script>
app.directive('specialChar', function() {
return {
require: 'ngModel',
ngModel.$validators.specialChar = function(value) {
};
};
});
</script>
In this example:
The specialChar custom directive validates that the password contains at least one special
character using a regular expression.
Select Box
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 39
Web Technologies 22MCA24
AngularJS provides two primary directives for creating select (dropdown) boxes: ng-options and
ng-repeat. The ng-options directive is specifically designed for filling a dropdown list with
options, making it the preferred choice when working with arrays or objects.
If you want to create a dropdown list, based on an object or an array in AngularJS, you should
use the ng-options directive:
Code:
<!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) {
});
</script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 40
Web Technologies 22MCA24
<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:
Code:
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<select>
</select>
</div>
<script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 41
Web Technologies 22MCA24
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.
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"}
];
<!DOCTYPE html>
<html>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></
script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 42
Web Technologies 22MCA24
<body>
<p>Select a car:</p>
<select ng-model="selectedCar">
</select>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = [
];
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 43
Web Technologies 22MCA24
});
</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>
Using ng-options:
<!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>
</div>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 44
Web Technologies 22MCA24
<script>
app.controller('myCtrl', function($scope) {
$scope.cars = [
];
});
</script>
<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>
AngularJS Tables
In AngularJS, tables are typically created using the <table> element combined with AngularJS
directives like ng-repeat for dynamically displaying data from an array.
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 45
Web Technologies 22MCA24
<!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>
</body>
</html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 46
Web Technologies 22MCA24
<!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>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 47
Web Technologies 22MCA24
<table>
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 48
Web Technologies 22MCA24
<!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>
<table>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 49
Web Technologies 22MCA24
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
<!DOCTYPE html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 50
Web Technologies 22MCA24
<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>
<table>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 51
Web Technologies 22MCA24
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 52
Web Technologies 22MCA24
<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>
<table>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 53
Web Technologies 22MCA24
</tr>
</table>
</div>
<script>
$http.get("customers.php")
});
</script>
</body>
</html>
Model
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 54
Web Technologies 22MCA24
In AngularJS, the model is typically a JavaScript object that resides in the $scope. The controller
initializes and manages the model, while the view binds to it.
<!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) {
});
</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
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 55
Web Technologies 22MCA24
The binding goes both ways. If the user changes the value inside the input field, the AngularJS
property will also change its value:
Code:
<!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) {
});
</script>
<p>Change the name inside the input field, and you will see the name in the header changes
accordingly.</p>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 56
Web Technologies 22MCA24
</body>
</html>
The ng-model directive can provide type validation for application data (number, e-mail,
required)
Code:
<!DOCTYPE html>
<html>
<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>
Application Status
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 57
Web Technologies 22MCA24
The ng-model directive can provide status for application data (valid, dirty, touched, error):
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
Email:
<h1>Status</h1>
</form>
</body>
</html>
CSS Classes
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 58
Web Technologies 22MCA24
The ng-model directive provides CSS classes for HTML elements, depending on their status:
<!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>
</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>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 59
Web Technologies 22MCA24
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
Data binding in AngularJS is the synchronization between the model and the view.
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";
});
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 60
Web Technologies 22MCA24
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
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<p ng-bind="firstname"></p>
</div>
<script>
app.controller('myCtrl', function($scope) {
$scope.firstname = "John";
$scope.lastname = "Doe";
});
</script>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 61
Web Technologies 22MCA24
<p>Use the ng-bind directive to bind the innerHTML of an element to a property in the data
model.</p>
</body>
</html>
You can also use double braces {{ }} to display content from the model:
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.firstname = "John";
$scope.lastname = "Doe";
});
</script>
<p>You can use double braces to display content from the data model.</p>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 62
Web Technologies 22MCA24
</body>
</html>
Ms Yashaswini Y
Assistant Professor, Dept of MCA,NCEH Page 63