UNIT 5 - Ost
UNIT 5 - Ost
UNIT 5 - Ost
<body>
<h1>Sample Application</h1>
</body>
</html>
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.
Example
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angu
lar.min.js"></script>
<body>
<div ng-app="">
<p>My first expression: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS Directives
AngularJS directives are extended HTML attributes with the prefix ng-.
The ng-app directive initializes an AngularJS application.
The ng-model directive binds the value of HTML controls (input, select,
textarea) to application data.
Example
<div ng-app="" ng-init="firstName='John'">
</div>
MVC:
Model View Controller or MVC as it is popularly called, is a software design pattern
for developing web applications. A Model View Controller pattern is made up of the
following three parts −
• Model − It is the lowest level of the pattern responsible for maintaining data.
• View − It is responsible for displaying all or a portion of the data to the user.
• Controller − It is a software Code that controls the interactions between the
Model and View.
MVC is popular because it isolates the application logic from the user interface layer
and supports separation of concerns. The controller receives all requests for the
application and then works with the model to prepare any data needed by the view.
The view then uses the data prepared by the controller to generate a final presentable
response. The MVC abstraction can be graphically represented as follows.
The Model
The model is responsible for managing application data. It responds to the request
from view and to the instructions from controller to update itself.
The View
A presentation of data in a particular format, triggered by the controller's decision to
present the data. They are script-based template systems such as JSP, ASP, PHP
and very easy to integrate with AJAX technology.
The Controller
The controller responds to user input and performs interactions on the data model
objects. The controller receives input, validates it, and then performs business
operations that modify the state of the data model.
AngularJS is a MVC based framework. In the coming chapters, we will see how
AngularJS uses MVC methodology.
Filters- Built-inFilters:
AngularJS filter is a tool, which we can use to format the data. With this filter, the
user can see and modify according to the requirement. It is added in angular to
format the data that is being displayed on the view part.
Syntax of Filter
With pipe character (|), we can add filters in angularJS.
Syntax:
{{expression| name_of_filter}}
AngularJS Modules
A module in AngularJS is a container of the different parts of an
application such as controller, service, filters, directives, factories
etc. It supports separation of concern using modules.
AngularJS Directives
AngularJS lets you extend HTML with new attributes called Directives.
The ng-model directive binds the value of HTML controls (input, select,
textarea) to application data.
Example
<div ng-app="" ng-init="firstName='John'">
</div>
Built-in directives
Directives are classes that add additional behavior to elements in your Angular
applications. Use Angular's built-in directives to manage forms, lists, styles, and what
users see.
Many NgModules such as the RouterModule and the FormsModule define their own
attribute directives. The most common attribute directives are as follows:
Built-in directives use only public APIs. They do not have special access to any private
APIs that other directives can't access.
To add or remove a single class, use class binding rather than NgClass.
Using NgClass with an expression
On the element you'd like to style, add [ngClass] and set it equal to an expression. In
this case, isSpecial is a boolean set to true in app.component.ts.
Because isSpecial is true, ngClass applies the class of special to the <div>.
AngularJS enriches form filling and validation. We can use ng-click event to handle
the click button and use $dirty and $invalid flags to do the validation in a seamless
way. Use novalidate with a form declaration to disable any browser-specific
validation. The form controls make heavy use of AngularJS events. Let us have a
look at the events first.
Events
AngularJS provides multiple events associated with the HTML controls. For example,
ng-click directive is generally associated with a button. AngularJS supports the
following events −
• ng-click
• ng-dbl-click
• ng-mousedown
• ng-mouseup
• ng-mouseenter
• ng-mouseleave
• ng-mousemove
• ng-mouseover
• ng-keydown
• ng-keyup
• ng-keypress
• ng-change
ng-click
Reset data of a form using on-click directive of a button.
<input name = "firstname" type = "text" ng-model = "firstName"
required>
<input name = "lastname" type = "text" ng-model = "lastName"
required>
<input name = "email" type = "email" ng-model = "email" required>
<button ng-click = "reset()">Reset</button>
<script>
function studentController($scope) {
$scope.reset = function() {
$scope.firstName = "Mahesh";
$scope.lastName = "Parashar";
$scope.email = "MaheshParashar@tutorialspoint.com";
}
$scope.reset();
}
</script>
Validate Data
The following can be used to track error.
• $dirty − states that value has been changed.
• $invalid − states that value entered is invalid.
• $error − states the exact error.
Example
The following example will showcase all the above-mentioned directives.
testAngularJS.htm
Live Demo
<html>
<head>
<title>Angular JS Forms</title>
<script src =
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.m
in.js"></script>
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f2f2f2;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app = "mainApp" ng-controller =
"studentController">
<tr>
<td>Enter last name: </td>
<td><input name = "lastname" type = "text" ng-
model = "lastName" required>
<span style = "color:red" ng-show =
"studentForm.lastname.$dirty && studentForm.lastname.$invalid">
<span ng-show =
"studentForm.lastname.$error.required">Last Name is
required.</span>
</span>
</td>
</tr>
<tr>
<td>Email: </td><td><input name = "email" type
= "email" ng-model = "email" length = "100" required>
<span style = "color:red" ng-show =
"studentForm.email.$dirty && studentForm.email.$invalid">
<span ng-show =
"studentForm.email.$error.required">Email is required.</span>
<span ng-show =
"studentForm.email.$error.email">Invalid email address.</span>
</span>
</td>
</tr>
<tr>
<td>
<button ng-click = "reset()">Reset</button>
</td>
<td>
<button ng-disabled =
"studentForm.firstname.$dirty &&
studentForm.firstname.$invalid ||
studentForm.lastname.$dirty &&
studentForm.lastname.$invalid ||
studentForm.email.$dirty &&
studentForm.email.$invalid" ng-
click="submit()">Submit</button>
</td>
</tr>
</table>
</form>
</div>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('studentController', function($scope)
{
$scope.reset = function() {
$scope.firstName = "Mahesh";
$scope.lastName = "Parashar";
$scope.email =
"MaheshParashar@tutorialspoint.com";
}
$scope.reset();
});
</script>
</body>
</html>
Output
Open the file testAngularJS.htm in a web browser and see the result.
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>
AngularJS Forms
The HTML form is a collection of input controls where user can enter
the data. Here, you will learn how to display AngularJS form and
submit the data.
$http.post('/student/submitData', {
student:$scope.student })
.success(onSuccess)
.error(onError);
};
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:
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>