AWP UNIT 3
AWP UNIT 3
A module in AngularJS is a container that defines an application and its various components, such as
controllers, directives, services, and filters. It provides a way to organize code into reusable and
manageable units.
In AngularJS, a module is created using the angular.module function. It acts as the root container for
the entire application.
To create a module, use the angular.module function with the following syntax:
Explanation:
After defining a module, it must be linked to the HTML file using the ng-app directive.
Example:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
<script>
var app = angular.module("myApp", []);
</script>
</head>
<body>
<h2>AngularJS Module Example</h2>
</body>
</html>
Explanation:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
<script>
var app = angular.module("myApp", []);
app.controller("myController", function($scope) {
$scope.message = "Welcome to AngularJS!";
});
</script>
</head>
<body>
<div ng-controller="myController">
<h2>{{ message }}</h2>
</div>
</body>
</html>
Explanation:
• The controller assigns a message to $scope.message, which is displayed using {{ message }}.
Directives in AngularJS
Introduction to Directives
Directives in AngularJS are special attributes that extend HTML functionality. They allow developers
to create dynamic and reusable components by attaching behavior to HTML elements.
AngularJS provides built-in directives, and developers can also create custom directives to enhance
their applications.
2. Custom Directives – User-defined directives that add custom behavior to HTML elements.
1. ng-app Directive
Example:
2. ng-model Directive
Example:
<div ng-app="">
<input type="text" ng-model="name">
<p>Hello, {{ name }}!</p>
</div>
• As the user types, the text updates dynamically.
3. ng-bind Directive
Example:
<p ng-bind="message"></p>
4. ng-repeat Directive
Example:
Example:
Developers can create their own directives using the directive function.
Example:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
<script>
var app = angular.module("myApp", []);
app.directive("customMessage", function() {
return {
template: "<h2>This is a custom directive!</h2>"
};
});
</script>
</head>
<body>
<div custom-message></div>
</body>
</html>
Explanation:
• customMessage is a custom directive that adds a template to the element where it is used.
Advantages of Directives
Routes in AngularJS
Routing in AngularJS enables Single Page Applications (SPA) by allowing navigation between
different views without reloading the entire webpage. The ngRoute module is commonly used to
manage routes, enabling dynamic content updates based on the URL.
To use routing, include both the AngularJS core library and the angular-route.js file in your HTML file.
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-
route.js"></script>
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular-
route.js"></script>
<script>
var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "home.html"
})
.when("/about", {
templateUrl: "about.html"
})
.when("/contact", {
templateUrl: "contact.html"
})
.otherwise({
template: "<h2>404 Page Not Found</h2>"
});
});
</script>
</head>
<body>
<h1>AngularJS Routing Example</h1>
<a href="#!/">Home</a> |
<a href="#!/about">About</a> |
<a href="#!/contact">Contact</a>
<div ng-view></div>
</body>
</html>
home.html:
about.html:
<h2>About Us</h2>
<p>Information about our company.</p>
contact.html:
<h2>Contact Us</h2>
<p>Reach out via email at [email protected].</p>
1. Module Creation:
o The <div ng-view></div> is the placeholder where different views load dynamically.
1. Enables Single Page Applications (SPA) – No full-page reloads, making navigation faster.
2. Efficient Content Loading – Loads only required parts of the page dynamically.
3. Improved Code Organization – Separates views into different HTML files for maintainability.
Forms in AngularJS provide an easy way to collect user input and validate the data before processing.
AngularJS extends HTML forms with built-in validation features and provides two-way data binding
through the ng-model directive.
A form in AngularJS is defined using the <form> element, and input fields are bound using ng-model.
Example:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
</head>
<body>
<div ng-controller="FormController">
<form name="userForm">
<label>Name:</label>
<input type="text" name="userName" ng-model="user.name"
required>
<p ng-show="userForm.userName.$error.required">Name is
required.</p>
<label>Email:</label>
<input type="email" name="userEmail" ng-model="user.email"
required>
<p ng-show="userForm.userEmail.$error.required">Email is
required.</p>
<p ng-show="userForm.userEmail.$error.email">Invalid email
format.</p>
<label>Age:</label>
<input type="number" name="userAge" ng-model="user.age"
min="18" required>
<p ng-show="userForm.userAge.$error.required">Age is
required.</p>
<p ng-show="userForm.userAge.$error.min">Minimum age should be
18.</p>
<script>
var app = angular.module("myApp", []);
app.controller("FormController", function($scope) {
$scope.user = {};
});
</script>
</body>
</html>
AngularJS provides built-in validation properties that help track the state of form fields:
Property Description
$touched Returns true if the field has been focused and then left.
3. Commonly Used Form Validation Directives
Directive Description
You can use the ng-pattern directive to validate inputs with custom regular expressions.
<label>Phone Number:</label>
<input type="text" name="userPhone" ng-model="user.phone" ng-
pattern="/^[0-9]{10}$/" required>
<p ng-show="userForm.userPhone.$error.pattern">Phone number must be 10
digits.</p>
To show error messages only after the user interacts with the field, use $touched.
2. Real-time Feedback – Users get instant validation messages without submitting the form.
4. Improves User Experience – Forms are interactive and prevent incorrect submissions.
Data Binding in AngularJS
Data binding in AngularJS is the process of synchronizing the data between the model (JavaScript
variables) and the view (HTML). This eliminates the need for manually updating the DOM, making
applications more interactive and efficient.
One-way data binding updates the view when the model changes, but changes in the view do not
affect the model.
Example:
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
></script>
</head>
<body>
<div ng-controller="MyController">
<p>Enter Name: <input type="text" ng-model="name"></p>
<p>Hello, {{ name }}</p>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("MyController", function($scope) {
$scope.name = "John";
});
</script>
</body>
</html>
Data Binding in AngularJS
Data binding in AngularJS is the process of synchronizing the data between the model (JavaScript
variables) and the view (HTML). This eliminates the need for manually updating the DOM, making
applications more interactive and efficient.
Types of Data Binding in AngularJS
One-way data binding updates the view when the model changes, but changes in the view do not
affect the model.
Example:
html
CopyEdit
<!DOCTYPE html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
<div ng-controller="MyController">
</div>
<script>
app.controller("MyController", function($scope) {
$scope.name = "John";
});
</script>
</body>
</html>
Explanation:
• This updates the view whenever the model changes but does not update the model if the
view changes outside of ng-model.
Two-way data binding allows real-time synchronization between the model and the view. Any
change in the input field updates the model, and any change in the model updates the view
automatically.
Example:
html
CopyEdit
Explanation:
• The ng-model directive binds the input field with the age variable.
• When the user changes the value, both the model and view are updated simultaneously.
2. Simplifies Development – Less code to manage updates between the model and view.
Explanation:
• This updates the view whenever the model changes but does not update the model if the
view changes outside of ng-model.
Two-way data binding allows real-time synchronization between the model and the view. Any change
in the input field updates the model, and any change in the model updates the view automatically.
Example:
• The ng-model directive binds the input field with the age variable.
• When the user changes the value, both the model and view are updated simultaneously.
2. Simplifies Development – Less code to manage updates between the model and view.