Module-5-Web Application Frameworks
Module-5-Web Application Frameworks
AngularJS Introduction
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/
angular.min.js"></script>
AngularJS Example
Example explained:
AngularJS Expressions
AngularJS will resolve the expression, and return the result exactly where
the expression is written.
2
AngularJS expressions are much like JavaScript expressions: They
can contain literals, operators, and variables.
Change the color of the input box below, by changing its value:
3
Output:
AngularJS Modules
4
Creating a Module
<div ng-app="myApp">...</div>
<script>
</script>
Now you can add controllers, directives, filters, and more, to your
AngularJS application.
Adding a Controller
5
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:
6
myApp.js
var app = angular.module("myApp", []);
myCtrl.js
app.controller("myCtrl", function($scope) {
$scope.firstName = "John";
$scope.lastName= "Doe";
});
AngularJS modules reduces this problem, by keeping all functions local to the
module.
7
When to Load the Library
AngularJS Directives:
AngularJS Directives
8
The ng-model directive binds the value of HTML controls (input, select,
textarea) to application data.
The ng-app directive also tells AngularJS that the <div> element is the "owner"
of the AngularJS application.
Data Binding
In the next example two text fields are bound together with two ng-model
directives:
9
Repeating HTML Elements
10
The ng-app Directive
Normally, you will not use ng-init. You will use a controller or module instead.
In addition to all the built-in AngularJS directives, you can create your own
directives.
To invoke the new directive, make an HTML element with the same tag name
as the new directive.
11
ou can invoke a directive by using:
Element name
Attribute
Class
Comment
Element name
<w3-test-directive></w3-test-directive>
Attribute
<div w3-test-directive></div>
12
The legal restrict values are:
By default the value is EA, meaning that both Element names and attribute
names can invoke the directive
13
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
14
Validate User Input
Application Status
15
CSS Classes
ng-empty
ng-not-empty
ng-touched
ng-untouched
ng-valid
ng-invalid
ng-dirty
ng-pending
ng-pristine
16
AngularJS Data Binding
Data binding in AngularJS is the synchronization between the model and the
view.
Data Model
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:
Example
<p>First name: {{firstname}}</p>
17
Two-way Binding
AngularJS Controller
Because of the immediate synchronization of the model and the view, the
controller can be completely separated from the view, and simply concentrate
on the model data. Thanks to the data binding in AngularJS, the view will
reflect any changes made in the controller.
18
AngularJS Controllers:
AngularJS Controllers
19
Application explained:
Controller Methods
20
AngularJS HTML DOM
AngularJS has directives for binding application data to the attributes of HTML
DOM elements.
21
The ng-disabled Directive
Application explained:
<p>
<button disabled>Click Me!</button>
</p>
22
The ng-hide directive hides or shows an HTML element.
AngularJS Events:
AngularJS Events
You can add AngularJS event listeners to your HTML elements by using one or
more of these directives:
23
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
24
25
AngularJS Forms
Input Controls
input elements
select elements
button elements
textarea elements
Data-Binding
<input type="text" ng-model="firstname">
26
Checkbox
Radiobuttons
27
Radio buttons with the same ng-model can have different values, but only the
selected one will be used.
<!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:
<input type="radio" ng-model="myVar" value="dogs">Dogs
<input type="radio" ng-model="myVar" value="tuts">Tutorials
<input type="radio" ng-model="myVar" value="cars">Cars
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
<p>The ng-switch directive hides and shows HTML sections depending on the
value of the radio buttons.</p>
</body>
</html>
OUTPUT:
28
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
<option value="tuts">Tutorials
<option value="cars">Cars
</select>
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
29
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
<p>The ng-switch directive hides and shows HTML sections depending on the
value of the dropdown list.</p>
</body>
</html>
OUTPUT:
<div ng-app="myApp" ng-controller="formCtrl">
<form novalidate>
First Name:<br>
<input type="text" ng-model="user.firstName"><br>
Last Name:<br>
<input type="text" ng-model="user.lastName">
<br><br>
30
<button ng-click="reset()">RESET</button>
</form>
<p>form = {{user}}</p>
<p>master = {{master}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
$scope.master = {firstName: "John", lastName: "Doe"};
$scope.reset = function() {
$scope.user = angular.copy($scope.master);
};
$scope.reset();
});
</script>
OUTPUT:
31
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.
32
E-mail
AngularJS is constantly updating the state of both the form and the input fields.
They are all properties of the input field, and are either true or false.
33
$invalid The form content is not valid
$valid The form content is valid
$submitted The form is submitted
<!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:
<input name="myName" ng-model="myName" required>
<span ng-show="myForm.myName.$touched && myForm.myName.
$invalid">The name is required.</span>
</p>
<p>Address:
<input name="myAddress" ng-model="myAddress" required>
</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>
OUTPUT:
34
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>
<form ng-app="myApp" ng-controller="validateCtrl"
name="myForm" novalidate>
<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.
$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>
<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"
35
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>
36