Angular Js Sid21
Angular Js Sid21
DEPRTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
COURS E PRE-REQUISITES:
COURSE OBJECTIVES:
COURSE CONTENT:
SlNo. CONTENTS HOURS
1 Develop Angular JS program that allows user to input their first name and last name 2
and display their full name. Note: The default values for first name and last name may
be included in the program.
2 Develop an Angular JS application that displays a list of shopping items. Allow users to 2
add and remove items from the list using directives and controllers. Note: The default
values of items may be included in the program.
3 Develop a simple Angular JS calculator application that can perform basic 2
mathematical operations (addition, subtraction, multiplication, division) based on
user input.
4 Write an Angular JS application that can calculate factorial and compute square based 2
on given user input.
5 Develop AngularJS application that displays a details of students and their CGPA. Allow 2
users to read the number of students and display the count. Note: Student details may
be included in the program.
6 Develop an AngularJS program to create a simple to-do list application. Allow users to 2
add, edit, and delete tasks. Note: The default values for tasks may be included in the
program.
7 Write an AngularJS program to create a simple CRUD application (Create, Read, 2
Update, and Delete) for managing users.
8 Develop AngularJS program to create a login form, with validation for the username 2
and password fields.
9 Create an AngularJS application that displays a list of employees and their salaries. 2
Allow users to search for employees by name and salary. Note: Employee details may
be included in the program.
10 Create AngularJS application that allows users to maintain a collection of items. The 2
application should display the current total number of items, and this count should
automatically update as items are added or removed. Users should be able to add
items to the collection and remove them as needed. Note: The default values for items
may be included in the program.
11 Create AngularJS application to convert student details to Uppercase using angular 2
filters. Note: The default details of students may be included in the program.
12 Create an AngularJS application that displays the date by using date filter parameters. 2
TOTAL HOURS 24
Textbooks:
1. ShyamSeshadri, Brad Green – “AngularJS: Up and Running: Enhanced Productivity with Structured
Web Apps”, Apress, 0'Reilly Media,Inc.
2. AgusKurniawan – “AngularJS Programming by Example”, First Edition, PE Press, 2014
Web links and Video Lectures (e-Resources):
1. Introduction to Angular JS : https://www.youtube.com/watch?v=HEbphzK-0xE
the programming, designing in building the applications and guiding them innovative career
path.
2 The course outcome 2 has been mapped substantially to PO1, as Students apply the knowledge of
engineering fundamentals and principles of Web programming. Mapped moderately to PO2, as
students identify and review complex engineering problems using principles of Engineering Sciences.
Mapped moderately to PO3, as students design web applications that meet the specified needs with
appropriate consideration for the public, business. The course outcome is mapped moderately to
PO5, as Students can select the modem tools like Visual Studio Code, Atom in building the dynamic
web applications. Mapped substantially to PO12, as students engage in lifelong learing in the context
of building web applications using upgraded web technology. Mapped moderately to PSO 2 as
students apply the programming, designing in building the applications and guiding them innovative
career path.
3 The course outcome 3 has been mapped substantially to PO1, as Students apply the knowledge of
engineering fundamentals and principles of Web programming. Mapped moderately to PO2, as
students identify and review complex engineering problems using principles of Engineering Sciences.
Mapped moderately to PO3, as students design web applications that meet the specified needs with
appropriate consideration for the public, business. The course outcome is mapped moderately to
PO5, as Students can select the modern tools like Visual Studio Code, Atom in building the interactive
web applications. Mapped substantially to PO12, as students engage in lifelong learning in the context
of building web applications using upgraded web technology. Mapped moderately to PSO 2 as
students apply the programming, designing in building the applications and guiding them innovative
career path.
4 The course outcome 4 has been mapped substantially to PO1, as Students apply the knowledge of
engineering fundamentals and principles of Web programming. Mapped moderately to POs, as
students identify and review complex engineering problems using principles of Engineering Sciences.
Mapped moderately to PO3, as students design web applications that meet the specified needs with
appropriate consideration for the public, business. The course outcome is mapped moderately to
PO5, as Students can select the modern tools like Visual Studio Code, Atom for implementing Angular
filters in web applications. Mapped substantially to PO12, as students engage in lifelong learning in
the context of building web applications using upgraded web technology. Mapped moderately to PSO
2 as students apply the programming, designing in building the applications and guiding them
innovative career path.
5 The course outcome 5 has been mapped substantially to PO1, as Students apply the knowledge of
engineering fundamentals and principles of web programming. Mapped moderately to PO2, as
students identify and review complex engineering problems using principles of Engineering Sciences.
Mapped moderately to PO3, as students design web applications that meet the specified needs with
appropriate consideration for the public business. The course outcome is mapped substantially to
PO5, as Students select the modern tools like visual Studio code, Atom for developing better web
applications. Mapped substantially to PO 12, as students engage in lifelong learning in the context of
building web applications using upgraded web technology. Mapped moderately to PSO 2 as students
apply the programming, designing in building the applications and guiding them innovative career
path.
Program1: Develop Angular JS program that allows user to input their first name and last
name and display their full name. Note: The default values for first name and last name
may be included in the program.
Description: Users can input their first and last names into an AngularJS program, which
then displays their entire name. 'Srinivasa ' and ' Ramanujan ' are the default values assigned
to the first and last names. These values are modifiable in the controller (myCtrl) as needed.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<h1>Full Name Program</h1>
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName = "Srinivasa";
$scope.lastName = "Ramanujan";
});
</script>
</body>
</html>
OUTPUT:
Program 2: Develop an Angular JS application that displays a list of shopping items. Allow
users to add and remove items from the list using directives and controllers. Note: The
default values of items may be included in the program.
Description: AngularJS application that shows a shopping list and lets users edit the list by
adding and removing items with controllers and directives. The shopping list is managed by
this AngularJS application using a controller called myCtrl. The item list is displayed using
ng-repeat, and there is an input field for adding new items. When an item is clicked on the
"X" text next to each one, it is removed from the list. Clicking the "Add Item" button adds
the entered item to the list. The items in the default list are "Eggs," "Bread," and "Milk.".
Exp2.html
<!DOCTYPE html>
<html ng-app="shoppingApp">
<head>
<title>Shopping List App</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="D:\Sid21\Angular Js\app2.js"></script>
</head>
<body ng-controller="ShoppingController">
<h1>Shopping List</h1>
<div>
<label for="newItem">Add Item:</label>
<input type="text" id="newItem" ng-model="newItem" />
<button ng-click="addItem()">Add</button>
</div>
<ul>
<li ng-repeat="item in shoppingItems">
{{ item }}
<button ng-click="removeItem($index)">Remove</button>
</li>
</ul>
</body>
</html>
Department of Computer Science and Engineering 3
BASAVAKALYAN ENGINEERING COLLEGE, BASAVAKALYAN. AngulaJS Lab Manual(21CSL581)
Exp2.js
angular.module('shoppingApp', [])
.controller('ShoppingController', function ($scope) {
$scope.shoppingItems = ['Apples', 'Bananas', 'Bread', 'Milk'];
$scope.newItem = '';
$scope.addItem = function () {
if ($scope.newItem) {
$scope.shoppingItems.push($scope.newItem);
$scope.newItem = ''; // Clear the input field
}
};
Output:-
Program 3: Develop a simple Angular JS calculator application that can perform basic
mathematical operations (addition, subtraction, multiplication, division) based on user
input.
Description: This AngularJS calculator app has two input fields where users can enter
numbers, a dropdown menu where users can choose the operation to perform, and a
'Calculate' button where users can actually perform the calculation. The myCtrl handles the
calculation based on the selected operation (addition, subtraction, multiplication, division)
and displays the result.
<!DOCTYPE html>
<html>
<head>
<style>
#f1 { font-size:22px }
</style>
<title>Calculator WebApp</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
OUTPUT:
.result {
margin-top: 10px;
font-weight: bold;
}
</style>
</head>
<body>
<div ng-controller="CalculatorController">
<h2>AngularJS Math Calculator</h2>
<label for="number">Number:</label>
<input type="number" id="number" ng-model="number" />
<div class="result">
Factorial: {{ factorialResult }}
</div>
<div class="result">
Department of Computer Science and Engineering 9
BASAVAKALYAN ENGINEERING COLLEGE, BASAVAKALYAN. AngulaJS Lab Manual(21CSL581)
Square: {{ squareResult }}
</div>
</div>
<script>
var app = angular.module('calculatorApp', []);
$scope.calculateFactorial = function () {
if ($scope.number < 0) {
alert("Factorial is not defined for negative numbers.");
} else {
$scope.factorialResult = 1;
for (var i = 1; i <= $scope.number; i++) {
$scope.factorialResult *= i;
}
}
};
$scope.calculateSquare = function () {
$scope.squareResult = $scope.number * $scope.number;
};
});
</script>
</body>
</html>
Output:-
Program 5: Develop AngularJS application that displays a details of students and their CGPA.
Allow users to read the number of students and display the count. Note: Student details
may be included in the program.
Program5.html
Program.js
<!DOCTYPE html>
<html >
<head>
<title>Student Details with CGPA</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<h2>Student Details</h2>
<p>Total number of students: {{ students.length }}</p>
<table border="1">
<tr>
<th>Name</th>
<th>CGPA</th>
</tr>
<tr ng-repeat="student in students">
<td>{{ student.name }}</td>
<td>{{ student.cgpa }}</td>
</tr>
</table>
</div>
<script src="Program5.js"> </script>
</body>
</html>
OUTPUT:
Program 6: Develop an AngularJS program to create a simple to-do list application. Allow users
to add, edit, and delete tasks. Note: The default values for tasks may be included in the program.
Description: Users can add, edit, and remove tasks from a to-do list application using the
AngularJS program. Tasks are displayed in an unordered list (<ul>). Users can add a new task by
entering the task name and clicking the "Add Task" button. Each task has "Edit" and "Delete"
buttons. Clicking the "Edit" button allows users to edit the task name, and they can save or cancel
the edit. Clicking the "Delete" button removes the task from the list
<!DOCTYPE html>
<html ng-app="ToDoApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-controller="ToDoController">
<h2>To-Do List</h2>
<div>
<input type="text" ng-model="newTask" placeholder="Add a new task">
<button ng-click="addTask()">Add</button>
</div>
<ul>
<li ng-repeat="task in tasks">
{{ task.name }}
<span ng-show="task.editing">
<input type="text" ng-model="task.name" ng-blur="saveTask(task)">
</span>
<span ng-show="!task.editing">
<button ng-click="editTask(task)">Edit</button>
<button ng-click="removeTask(task)">Delete</button>
</span>
</li>
</ul>
<script>
var app = angular.module('ToDoApp', []);
app.controller('ToDoController', function($scope) {
//Default Tasks
$scope.tasks = [{ name: 'Attend Class'},
OUTPUT:
Program 7: Write an AngularJS program to create a simple CRUD application (Create, Read,
Update, and Delete) for managing users.
Description: The CRUD (Create, Read, Update, Delete) application for managing users
written in AngularJS. A table shows the user's details. The "create" button can be used to
add new users. There are "Edit" and "Delete" buttons for each user. Clicking "Edit" allows
users to edit the user's name and email. Clicking "Delete" removes the user from the list.
<!DOCTYPE html>
<html ng-app="userApp">
<head>
<title>AngularJS CRUD Application for Users</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
</head>
<body ng-controller="UserController">
<h1>User Management </h1>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tr ng-repeat="user1 in users">
<td>{{user1.name}}</td>
<td>{{user1.email}}</td>
<td>
<button ng-click="editUser(user1)">Edit</button>
<button ng-click="deleteUser(user1)">Delete</button>
</td>
</tr>
</table>
<hr>
<h2>Create User</h2>
$scope.users = [
{name: 'Suma K', email: '[email protected]' },
{ name: 'Ratan Kumar', email: '[email protected]' }
];
$scope.newUser = {};
$scope.createUser = function() {
$scope.users.push($scope.newUser);
$scope.newUser = {};
};
$scope.editUser = function(user) {
$scope.editedUser = user;
};
$scope.updateUser = function() {
$scope.editedUser = {};
};
$scope.deleteUser = function(user) {
$scope.users.splice($scope.users.indexOf(user), 1);
};
});
Program 8: Develop AngularJS program to create a login form, with validation for the
username and password fields.
Description: For a login form with validation for the username and password. The password
is required to be alphanumeric and 8 characters long. The form uses AngularJS validation
with the required attribute for both the username and password fields. The password field
also uses the ng-pattern attribute to enforce the alphanumeric requirement and a minimum
length of 8 characters. Error messages are displayed based on the validation status. The
"Login" button is disabled until the form is valid. The ng-click directive triggers the login()
function, which can contain the actual login logic. For simplicity, it just sets isLoggedIn to
true in this example.
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Login Example</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
</head>
<body>
<div ng-controller="LoginController as loginCtrl">
<form ng-submit="loginCtrl.login()">
<label for="username">Username:</label>
<input type="text" id="username" ng-model="loginCtrl.username" required>
<label for="password">Password:</label>
<input type="password" id="password" ng-model="loginCtrl.password" required>
<button type="submit">Login</button>
</form>
<script>
var app = angular.module('myApp', []);
this.login = function() {
// Perform validation (you can replace this with your own logic)
if (this.username === 'user' && this.password === 'pass') {
// Successful login
this.errorMessage = '';
alert('Login successful!');
} else {
// Failed login
this.errorMessage = 'Invalid username or password';
}
};
});
</script>
</body>
</html>
OUTPUT:
Program 9: Create an AngularJS application that displays a list of employees and their salaries.
Allow users to search for employees by name and salary. Note: Employee details may be
included in the program.
Description: The AngularJS application that displays a list of employees and their salaries.
Employees are displayed in an unordered list (<ul>). Users can search for employees by name
and salary using the ng-model directive. The filter pipe is used to filter employees based on the
search criteria (name and salary). Employee salaries are formatted using the number filter to
display two decimal places.
<!DOCTYPE html>
<html ng-app="employeeApp">
<head>
<title>Employee List</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="EmployeeController">
<h2>Employee List</h2>
<label>Search by Name:</label>
<input type="text" ng-model="searchName" />
<label>Search by Salary:</label>
<input type="number" ng-model="searchSalary" />
<button ng-click="searchEmployees()">Search</button>
<ul>
<li ng-repeat="employee in filteredEmployees">
{{ employee.name }} - {{ employee.salary}}
</li>
</ul>
<script>
angular.module('employeeApp', [])
.controller('EmployeeController', function ($scope) {
$scope.employees = [
{ name: 'Keerthana SS', salary: 50000 },
{ name: 'Rakshith B', salary: 60000 },
{ name: 'Santhosh', salary: 70000 },
{ name: 'Radhika Pai', salary: 55000 },
{ name: 'Skandha', salary: 80000 }
];
$scope.filteredEmployees = $scope.employees;
$scope.searchEmployees = function () {
$scope.filteredEmployees = $scope.employees.filter(function (employee) {
return (
(employee.name.toLowerCase().includes($scope.searchName.toLowerCase()) ||
!$scope.searchName) &&
(employee.salary == $scope.searchSalary || !$scope.searchSalary)
);
});
};
});
</script>
</body>
</html>
Program 10: Create AngularJS application that allows users to maintain a collection of items.
The application should display the current total number of items, and this count should
automatically update as items are added or removed. Users should be able to add items to the
collection and remove them as needed. Note: The default values for items may be included in
the program.
Description: The AngularJS application that allows users to maintain a collection of items. The
application displays the current total number of items, and this count updates automatically
as items are added or removed. The total number of items is displayed using {{ items.length }}.
Items are displayed in an unordered list (<ul>). Users can add a new item by entering the item
name and clicking the "Add Item" button. Each item has a "Remove" button that allows users
to remove the item from the collection.
<!DOCTYPE html>
<html>
<head>
<title>Item Collection Management</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<div ng-controller="ItemController">
<h2>Item Collection</h2>
<p>Total number of items: {{ items.length }}</p>
<ul>
<li ng-repeat="item in items">
{{item.name }}
<button ng-click="removeItem(item)">Remove</button>
</li>
</ul>
<div>
<label>New Item: </label>
<input type="text" ng-model="newItemName">
<button ng-click="addItem()">Add Item</button>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('ItemController', function ($scope) {
// Default items
$scope.items = [
{ name: 'Apple'},
{ name: 'Banana'},
{ name: 'Orange'}];
$scope.newItemName = '';
$scope.addItem = function () {
if ($scope.newItemName) {
$scope.items.push({ name: $scope.newItemName });
$scope.newItemName = '';
}
};
$scope.removeItem = function (item) {
var index = $scope.items.indexOf(item);
if (index !== -1) {
$scope.items.splice(index, 1);
}
};
});
</script>
</body>
</html>
Syntax: {{ string | uppercase}} The uppercase filter is applied to each property of the student
details to convert them to uppercase. The default student details are defined in the
controller using the $scope.students array. The details are displayed in a table using ng-
repeat to iterate over the students
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Student Details Converter</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
</head>
<body>
<div ng-controller="StudentController as studentCtrl">
<label for="studentName">Enter Student Name (lowercase):</label>
<input type="text" id="studentName" ng-model="studentCtrl.student.name">
<script>
var app = angular.module('myApp', []);
OUTPUT:
Program 12: Create an AngularJS application that displays the date by using date filter
parameters
Description: AngularJS date filter is used to convert a date into a specified format. When
the date format is not specified, the default date format is ‘MMM d, yyyy’.
Syntax: {{ date | date : format : timezone }} Parameter Values: The date filter contains
format and timezone parameters which is optional.
In this program the user can select a date format from a dropdown list. The ng-change
directive is used to trigger the updateDate function whenever the selected format changes.
The updateDate function uses the AngularJS $filter service to format the current date based
on the selected format. Formatted date is then displayed in the HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS Date Display</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="myController">
<h2>Date Display</h2>
<label>Date Format:
<select ng-model="selectedFormat" ng-change="updateDate()">
<option value="fullDate">Full Date</option>
<option value="shortDate">Short Date</option>
<option value="mediumTime">Medium Time</option>
<option value="shortTime">short Time</option>
<option value="yyyy-MM-dd HH:mm:ss">Custom Format (yyyy-MM-dd
HH:mm:ss)</option>
</select>
</label>
<p>Selected Date Format: {{ selectedFormat }}</p>
<p>Formatted Date: {{ formattedDate }}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $filter) {
$scope.selectedFormat = 'fullDate'; // Default date format
$scope.updateDate = function () {
var currentDate = new Date();
$scope.formattedDate = $filter('date')(currentDate, $scope.selectedFormat);
};
// Initial date update
$scope.updateDate();
});
</script>
</body>
</html>