AJS PRGM 5-10
AJS PRGM 5-10
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.
Program:
<html ng-app="studentApp">
<head>
<title>AngularJS Student Details</title>
<script src="Ang.js"></script>
</head>
<body ng-controller="studentController">
<h2>Student Details</h2> Student Name:
<input type="text" ng-model="name" /> CGPA:
<input type="number" ng-model="cgpa" ng-min="1" ng-max="10"/>
<button ng-click="addStudent()">Add Student</button>
<p>Total Students: {{ students.length }}</p>
<ul>
<li ng-repeat="student in students">
{{ student.name }} - CGPA: {{ student.cgpa }}
</li>
</ul>
<script>
var app = angular.module('studentApp', []); app.controller('studentController', function
($scope) {
$scope.students = [];
$scope.addStudent = function () { if ($scope.name && $scope.cgpa) {
$scope.students.push({
name: $scope.name, cgpa:
$scope.cgpa
});
// Clear the input fields
$scope.name = '';
$scope.cgpa = '';
}
};
});
</script>
</body>
</html>
Output:
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
Program:
<html ng-app="todoApp">
<head>
<title>AngularJS Todo List</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body ng-controller="todoController">
<h1>Todo List</h1>
<!-- Form for adding a new task -->
<form ng-submit="addTask()"> Task:
<input type="text" ng-model="newTask" required>
<button type="submit">Add Task</button>
</form>
<br>
<!-- Table to display task information -->
<table>
<thead>
<tr>
<th>Task</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="task in tasks">
<td>{{ task }}</td>
<td>
<button ng-click="editTask($index)">Edit</button>
<button ng-click="deleteTask($index)">Delete</button>
</td>
</tr>
</tbody>
</table>
<!-- Edit Task Modal -->
<div ng-if="editingTaskIndex !== null">
<h2>Edit Task</h2> Task:
<input type="text" ng-model="tasks" required>
<br>
<button ng-click="saveEdit()">Save</button>
<button ng-click="cancelEdit()">Cancel</button>
</div>
<script>
var app = angular.module('todoApp', []);
app.controller('todoController', function ($scope) {
$scope.tasks = [ 'Task 1',
'Task 2',
'Task 3'
];
$scope.newTask = '';
$scope.editingTaskIndex = null;
$scope.addTask = function () {
$scope.tasks.push($scope.newTask);
$scope.newTask = '';
};
$scope.editTask = function (index) {
// Prompt for updated task with validation
var updatedTask = prompt('Enter updated task:');
// Check if the user pressed cancel if (updatedTask !== null) {
// Update the task
$scope.tasks.splice(index, 1, updatedTask);
}
};
Output:
<td>
<button ng-click="editUser(user)">Edit</button>
<button ng-click="deleteUser(user)">Delete</button>
</td>
</tr>
</tbody>
</table>
<script>
var app = angular.module('crudApp', []);
app.controller('crudController', function ($scope) {
$scope.users = [
{ name: 'Ram', age: 25 },
{ name: 'Sam', age: 30 },
];
$scope.addUser = function () {
$scope.users.push({ name: $scope.name, age: $scope.age });
$scope.name = '';
$scope.age = '';
};
$scope.editUser = function (user) {
var index = $scope.users.indexOf(user);
// Prompt for updated values with validation
var updatedName = prompt('Enter updated name:', user.name); var updatedAge =
prompt('Enter updated age:', user.age);
// Check if the user pressed cancel
if (!(updatedName == null && updatedAge == null) ){
// Update the user
var updatedUser = { name: updatedName, age: parseInt(updatedAge)
};
$scope.users.splice(index, 1, updatedUser);
}
};
Output:
8. Develop AngularJS program to create a login form, with validation for the
username and password fields.
Program:
<html ng-app="loginApp">
<script src="Ang.js"></script>
<body ng-controller="loginController">
<h1>Login Form</h1>
<!-- Form for login with validation -->
<form ng-submit="login()"> Username
<input type="text" ng-model="username" required>
<br> Password
<input type="password" ng-model="password" required>
<br>
<button type="submit">Login</button>
</form>
<script>
var app = angular.module('loginApp', []); app.controller('loginController', function
($scope) {
$scope.login = function () {
// Check if username is "Ram" and password is "Ram"
if ($scope.username == 'ram' && $scope.password == 'ram') { alert('Login successful');
// Add further logic for successful login
} else {
alert('Login failed. Invalid username or password.');
// Add logic for failed login
}
};
});
</script>
</body>
</html>
Output:
</body>
</html>
Output:
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.
Program:
<html ng-app="itemApp">
<script src="Ang.js"></script>
<body ng-controller="itemController">
<h2>Item Collection</h2>
Add New Item:
<input type="text" ng-model="newItem" />
<button ng-click="addItem()">Add Item</button>
<ul>
</ul>
<li ng-repeat="item in items track by $index">
{{ item }}
<button ng-click="removeItem($index)">Remove</button>
</li>
<p>Total Items: {{ items.length }}</p>
<script>
var app = angular.module('itemApp', []);
app.controller('itemController', function ($scope) {
$scope.items = ['Item 1', 'Item 2', 'Item 3']; // Default items
$scope.newItem = '';
$scope.addItem = function () { if ($scope.newItem) {
$scope.items.push($scope.newItem);
$scope.newItem = ''; // Clear the input field
}
};
$scope.removeItem = function (index) {
$scope.items.splice(index, 1);
};
});
</script>
</body>
</html>
Output:
<body ng-controller="studentController">
<h2>Student Names</h2>
<script>
var app = angular.module('studentApp', []);
});
</script>
</body>
</html>
Output:
12. Create an AngularJS application that displays the date by using date filter
parameters
Program:
<html ng-app="dateApp">
<head>
<title>Date Display Application</title>
<script src="Ang.js"></script>
</head>
<body ng-controller="dateController">
<h2>Date Display</h2>
<!-- Display the current date with various filter parameters -->
<p>Default Format: {{ currentDate | date }}</p>
<p>Custom Format (yyyy-MM-dd): {{ currentDate | date:'yyyy-MM-dd' }}</p>
<p>Short Date: {{ currentDate | date:'shortDate' }}</p>
<p>Full Date: {{ currentDate | date:'fullDate' }}</p>
<script>
var app = angular.module('dateApp', []); app.controller('dateController', function ($scope) {
$scope.currentDate = new Date();
});
</script>
</body>
</html>
Output: