Anjularjs Programs Revisedcirc Syllabus
Anjularjs Programs Revisedcirc Syllabus
display their full name. Note: The default values for first name and last name may be
included in the program.
<!DOCTYPE html>
<html lang="en" ng-app="fullNameApp">
<head>
<meta charset="UTF-8">
<title>Full Name App</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
<div ng-controller="fullNameCtrl">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" ng-model="firstName" placeholder="Enter your
first name">
<script>
var app = angular.module('fullNameApp', []);
app.controller('fullNameCtrl', function($scope) {
// Set default values
$scope.firstName = 'John';
$scope.lastName = 'Doe';
</body>
</html>
In this example:
In this example:
The ng-app directive defines the AngularJS application module named "fullNameApp."
The ng-controller directive attaches the "fullNameController" controller to the
specified HTML element, in this case, the div element.
Two input fields allow the user to input their first name and last name, and their values
are bound to the $scope.firstName and $scope.lastName variables using ng-model.
The ng-click directive triggers the displayFullName function when the "Display Full
Name" button is clicked.
The ng-if directive is used to conditionally display the full name only if it is defined.
Remember to include the AngularJS library in your project, either by downloading it and
hosting it locally or by using a CDN, as shown in the example.
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
<!DOCTYPE html>
<html lang="en" ng-app="shoppingApp">
<head>
<meta charset="UTF-8">
<title>Shopping App</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
</head>
<body>
<div ng-controller="ShoppingController as shoppingCtrl">
<h2>Shopping List</h2>
<ul>
<li ng-repeat="item in shoppingCtrl.items">
{{ item.name }} - {{ item.price | currency }}
<button ng-click="shoppingCtrl.removeItem(item)">Remove</button>
</li>
</ul>
<form ng-submit="shoppingCtrl.addItem()">
<label for="itemName">Item Name:</label>
<input type="text" id="itemName" ng-model="shoppingCtrl.newItemName"
required>
<script>
angular.module('shoppingApp', [])
.controller('ShoppingController', function () {
var vm = this;
// Default items
vm.items = [
{ name: 'Item 1', price: 10 },
{ name: 'Item 2', price: 20 },
{ name: 'Item 3', price: 30 }
];
vm.newItemName = '';
vm.newItemPrice = '';
vm.addItem = function () {
if (vm.newItemName && vm.newItemPrice) {
vm.items.push({
name: vm.newItemName,
price: parseFloat(vm.newItemPrice)
});
vm.newItemName = '';
vm.newItemPrice = '';
}
};
</body>
</html>
3. Develop a simple Angular JS calculator application that can perform basic
mathematical operations (addition, subtraction, multiplication, division) based
on user input.
Create a new HTML file and include the AngularJS library. You can download it or
include it from a CDN.
<!DOCTYPE html>
<html lang="en" ng-app="calculatorApp">
<head>
<meta charset="UTF-8">
<title>AngularJS Calculator</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<style>
/* Add some basic styling if needed */
input {
width: 50px;
margin: 5px;
}
</style>
</head>
<body>
<div ng-controller="CalculatorController">
<!-- Calculator UI goes here -->
<!-- Inside the div with ng-controller="CalculatorController" -->
<input type="text" ng-model="result" readonly>
<br>
<input type="button" value="1" ng-click="appendToExpression('1')">
<input type="button" value="2" ng-click="appendToExpression('2')">
<input type="button" value="3" ng-click="appendToExpression('3')">
<input type="button" value="+" ng-click="appendToExpression('+')">
<br>
<br>
<br>
angular.module('calculatorApp', [])
.controller('CalculatorController', function ($scope) {
// Controller logic goes here
// Inside the CalculatorController
$scope.result = '';
$scope.appendToExpression = function (value) {
$scope.result += value;
};
$scope.clearExpression = function () {
$scope.result = '';
};
$scope.evaluateExpression = function () {
try {
$scope.result = eval($scope.result).toString();
}
catch (e) {
$scope.result = 'Error';
}
};
});
4. Write an Angular JS application that can calculate factorial and compute square
based on given user input.
<!DOCTYPE html>
<html ng-app="calculatorApp">
<head>
<title>AngularJS Calculator</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
</head>
<body ng-controller="calculatorController">
<h1>Factorial and Square Calculator</h1>
<label for="numberInput">Enter a number:</label>
<input type="number" id="numberInput" ng-model="userInput" />
<br />
<button ng-click="calculateFactorial()">Calculate Factorial</button>
<button ng-click="calculateSquare()">Calculate Square</button>
<br />
<p ng-show="result">Result: {{ result }}</p>
<script>
var app = angular.module('calculatorApp', []);
app.controller('calculatorController', function ($scope) {
$scope.calculateFactorial = function () {
var num = $scope.userInput;
$scope.result = factorial(num);
};
$scope.calculateSquare = function () {
var num = $scope.userInput;
$scope.result = square(num);
};
function factorial(n) {
if (n === 0 || n === 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
function square(n) {
return n * n;
}
});
</script>
</body>
</html>
This example defines an AngularJS application with a controller ( calculatorController)
that has two functions, calculateFactorial and calculateSquare. The HTML template
includes an input field for the user to enter a number and two buttons to trigger the
respective calculations. The results are displayed below the buttons.
<!DOCTYPE html>
<html ng-app="studentApp">
<head>
<title>Student Details</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-controller="StudentController">
<h2>Student Details</h2>
<div ng-show="countDisplayed">
<li>
<strong>CGPA:</strong> {{ student.cgpa }}
</li>
</ul>
</div>
<script>
angular.module('studentApp', [])
$scope.students = [];
$scope.countDisplayed = false;
$scope.displayStudentCount = function () {
$scope.students = generateStudentDetails($scope.numberOfStudents);
$scope.studentCount = $scope.students.length;
$scope.countDisplayed = true;
};
function generateStudentDetails(num) {
students.push({
cgpa: generateRandomCGPA()
});
}
return students;
function generateRandomCGPA() {
});
</script>
</body>
</html>
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<style>
.completed {
text-decoration: line-through;
</style>
</head>
<body>
<div ng-controller="TodoController as todoCtrl">
<h2>Todo List</h2>
<form ng-submit="todoCtrl.addTask()">
</form>
<ul>
<button ng-click="todoCtrl.editTask(task)">Edit</button>
<button ng-click="todoCtrl.deleteTask(task)">Delete</button>
</li>
</ul>
</div>
<script>
angular.module('todoApp', [])
.controller('TodoController', function () {
todoCtrl.tasks = [
];
todoCtrl.addTask = function () {
if (todoCtrl.newTask) {
todoCtrl.newTask = '';
};
task.title = updatedTask;
};
todoCtrl.tasks.splice(index, 1);
};
});
</script>
</body>
</html>
<head>
<meta charset="UTF-8">
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<style>
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
th, td {
padding: 8px;
text-align: left;
th {
background-color: #f2f2f2;
form {
margin-top: 20px;
</style>
</head>
<body ng-controller="UserController">
<h2>User Management</h2>
<form>
<label for="name">Name:</label>
<label for="email">Email:</label>
</form>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<td>
<button ng-click="editUser(user)">Edit</button>
<button ng-click="deleteUser(user)">Delete</button>
</td>
</tr>
</table>
<script>
angular.module('userApp', [])
$scope.users = [
{ name: 'John Doe', email: 'john@example.com' },
];
$scope.newUser = {};
$scope.addUser = function () {
$scope.users.push(angular.copy($scope.newUser));
$scope.newUser = {};
};
$scope.newUser = angular.copy(user);
$scope.deleteUser(user);
};
$scope.users.splice(index, 1);
};
});
</script>
</body>
</html>
8. DevelopAngularJS program to create a login form, with validation for the username
and password fields.
<!DOCTYPE html>
<html ng-app="loginApp">
<head>
<title>Login Form</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<style>
.error {
color: red;
</style>
</head>
<body>
<div ng-controller="loginController">
<h2>Login Form</h2>
<label for="username">Username:</label>
<br>
<label for="password">Password:</label>
<br>
<button type="submit" ng-disabled="loginForm.$invalid">Login</button>
</form>
</div>
<script>
$scope.user = {};
$scope.submitForm = function () {
if ($scope.loginForm.$valid) {
alert('Login successful!');
} else {
};
});
</script>
</body>
</html>
<!DOCTYPE html>
<html ng-app="employeeApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="EmployeeController">
<h2>Employee Management</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Salary</th>
</tr>
<td>{{employee.name}}</td>
<td>{{employee.salary | currency}}</td>
</tr>
</table>
<script>
$scope.employees = [
];
});
</script>
</body>
</html>
<!DOCTYPE html>
<html ng-app="itemApp">
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body>
<div ng-controller="ItemController">
<h2>Item Collection</h2>
<form ng-submit="addItem()">
<label for="itemName">Item Name:</label>
</form>
<ul>
{{ item.name }}
<button ng-click="removeItem(item)">Remove</button>
</li>
</ul>
</div>
<script>
$scope.items = [
];
$scope.addItem = function () {
$scope.newItemName = '';
};
$scope.items.splice(index, 1);
};
});
</script>
</body>
</html>
Create AngularJS application to convert student details to Uppercase using angular filters.
Note: The default details of students may be included in the program.
<!DOCTYPE html>
<html ng-app="studentApp">
<head>
</head>
<body>
<div ng-controller="StudentController">
<h2>Student Details</h2>
<ul>
</li>
</ul>
</div>
<script>
angular.module('studentApp', [])
$scope.students = [
];
});
</script>
</body>
</html>
The controller initializes an array of students with their names and grades.
In the HTML, we use the ng-repeat directive to iterate over the students and display their details.
We use the uppercase filter to convert the student names and grades to uppercase.
Please note that you need to include the AngularJS library by adding the <script> tag with the
corresponding URL. Also, make sure that your application is served over HTTP or HTTPS to avoid any
cross-origin issues with loading external scripts.
12. Create an AngularJS application that displays the date by using date filter
arameters Include necessary HTML elementsand CSS for the above Angular
applications.
<!DOCTYPE html>
<html ng-app="dateApp">
<head>
<title>AngularJS Date Display</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
h1 {
color: #2196F3;
}
.date-container {
margin-top: 20px;
font-size: 24px;
}
</style>
</head>
<body ng-controller="DateController">
<div class="date-container">
<p>{{ currentDate | date: 'fullDate' }}</p>
</div>
<script>
angular.module('dateApp', [])
.controller('DateController', function ($scope, $interval) {
// Function to update the current date every second
function updateDate() {
$scope.currentDate = new Date();
}
$interval(updateDate, 1000);
});
</script>
</body>
</html>