Angular JS Lab Manual
Angular JS Lab Manual
Angular JS Lab Manual
V-Semester
1
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
ANGULAR JS
Course Code 21CSL581 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 0:0:2:0 SEE Marks 50
Credits 01 Total marks 100
Examination type (SEE) PRACTICAL
Course objectives:
To learn the basics of Angular JS framework.
To understand the Angular JS Modules, Forms, inputs, expression, data bindings and Filters
To gain experience of modern tool usage (VS Code, Atom or any other] in developing Web applications
Sl.NO Experiments
1 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.
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.
3 Develop a simple Angular JS calculator application that can perform basic mathematical operations
(addition, subtraction, multiplication, division) based on user input.
4
Write an Angular JS application that can calculate factorial and compute square based on given user input.
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.
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.
7 Write an AngularJS program to create a simple CRUD application (Create, Read, Update, and Delete) for
managing users.
8 Develop AngularJS program to create a login form, with validation for the username and password fields.
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.
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.
11 Create AngularJS application to convert student details to Uppercase using angular 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
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
NOTE 1: Sample Programs are given at the end for the students to practice
Note 2: Since Internet is not allowed during the exams it is not possible to use the
angular.min.js from online. Hence, instead of using the online script, please download the
offline angular.min.js from the link below
https://drive.google.com/file/d/18WE3NQA7p2isaLzSnMDJYXLWfWiW3NvT/view?usp=drive_link
and paste it inside the same directory from where the program is being run and use the below
script to include the angular.min.js.
<script type="text/javascript" src="angular.min.js"></script>
3
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
1. 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.
<!DOCTYPE html>
<html>
<title>
Angular JS Full Name Pgm
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.firstName="Harish Kumar"
$scope.lastName="B T"
});
</script>
</head>
<body ng-app="myApp">
<h2>Anjular JS Application to Display Full Name</h2>
<div ng-controller="myCntrl">
Enter First Name: <input type="text" ng-model="firstName"><br/>
Enter Last Name: <input type="text" ng-model="lastName"><br/>
Your Full Name: {{firstName +" "+ lastName}}
</div>
</body>
</html>
4
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
5
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>
Shopping Items Application
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.shoppingItems=['Apple','Mango','Banana','Grapes']
$scope.addItem=function(){
if($scope.newItem && $scope.shoppingItems.indexOf($scope.newItem)==-1)
{
$scope.shoppingItems.push($scope.newItem)
$scope.newItem=""
}
else
{
if($scope.newItem)
alert("This item is already there in the shopping list")
else
alert("Please enter an item to add")
}
}
$scope.removeItem=function(){
//console.log("function called")
if($scope.shoppingItems.indexOf($scope.selectItem)==-1)
{
alert("Please select an item to remove")
}
else{
var index=$scope.shoppingItems.indexOf($scope.selectItem)
$scope.shoppingItems.splice(index,1)
$scope.selectItem=""
}
}
});
</script>
6
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
<h2>Shopping Application</h2>
<h4>List of Shopping Items</h4>
<table border="1">
<tr>
<th>SLNO</th>
<th>Item</th>
</tr>
<tr ng-repeat="items in shoppingItems">
<td>{{$index+1}}</td>
<td>{{items}}</td>
</tr>
</table>
<br/>
<div>
Enter an Item to Add: <input type="text" ng-model="newItem">
<button ng-click="addItem()">Add Item</button>
</div>
<div>
Select an Item to Remove:
<select ng-model="selectItem" ng-options="item for item in shoppingItems"></select>
<button ng-click="removeItem()">Remove Item</button>
</div>
</div>
</body>
</html>
7
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
8
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>
AJS Simple Calculator
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("calcApp",[]);
app.controller("calcCntrl",function($scope)
{
$scope.num1=0
$scope.num2=0
$scope.result=0
$scope.operator="add"
$scope.compute=function(){
switch($scope.operator){
case 'add': $scope.result=$scope.num1 + $scope.num2
break
case 'sub': $scope.result=$scope.num1 - $scope.num2
break
case 'mul': $scope.result=$scope.num1 * $scope.num2
break
case 'div': if($scope.num2==0){
alert("Divide by zero error")
}
else{
$scope.result=$scope.num1/$scope.num2
}}}
});
</script>
</head>
<body ng-app="calcApp">
<h1>Angular JS Simple Calculator</h1>
<div ng-controller="calcCntrl">
9
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
10
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
4. Write an Angular JS application that can calculate factorial and compute square
based on given user input.
<!DOCTYPE html>
<html>
<title>
AJS Square and Factorial Application
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("mySqFct", []);
app.controller("mySqFctCntrl", function($scope){
$scope.num=0
$scope.result
$scope.factorial=function()
{
if($scope.num==0)
{
$scope.result=1
}
else{
$scope.fact=1
for(var i=$scope.num; i>=1; i--)
{
$scope.fact=$scope.fact*i
}
$scope.result=$scope.fact
}
}
$scope.square=function(){
$scope.result=$scope.num*$scope.num
}
});
</script>
</head>
<body ng-app="mySqFct">
<h1> Angular JS Factorial and Square Application</h1>
<div ng-controller="mySqFctCntrl">
Enter the Number: <input type="number" ng-model="num">
<button ng-click="factorial()">Compute Factorial</button>
11
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<br/>
{{result}}
</div>
</body>
</html>
Output:
12
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
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.
<!DOCTYPE html>
<html>
<title>Student Details Application</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("studDetailsApp",[]);
app.controller("studDetailsAppCntrl",function($scope){
$scope.studData=[]
$scope.generateData=function()
{
$scope.studData=[]
for(var i=1;i<=$scope.num;i++)
{
var stud={
"SLNO":i,
"NAME":'Student-'+i,
"CGPA":(Math.random()*10+1).toFixed(2)
}
$scope.studData.push(stud)
}
}
});
</script>
</head>
<body ng-app="studDetailsApp">
<h1>Student Details Application</h1>
<div ng-controller="studDetailsAppCntrl">
Enter the Number of Students to Generate the Data:
<input type="number" ng-model="num">
<button ng-click="generateData()">Generate</button>
<br/>
<table border="1" ng-show="studData.length>0">
<tr>
<th>SLNO</th>
<th>NAME</th>
<th>CGPA</th>
</tr>
<tr ng-repeat="student in studData">
<td>{{student.SLNO}}</td>
13
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<td>{{student.NAME}}</td>
<td>{{student.CGPA}}</td>
</tr>
</table>
<br/>
Number of Students={{studData.length}}
</div>
</body>
</html>
Output:
14
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
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.
<!DOCTYPE html>
<html>
<title>TO DO Application</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("toDoApp",[]);
app.controller("toDoAppCntrl",function($scope){
$scope.tasks=[
{'TITLE':'Task-1','COMPLETED':true,'EDITING':false},
{'TITLE':'Task-2','COMPLETED':false,'EDITING':false},
{'TITLE':'Task-3','COMPLETED':false,'EDITING':false}
]
$scope.addTask=function(){
if($scope.newTask)
{
var t={
'TITLE':$scope.newTask,
'COMPLETED':false,
'EDITING':false
}
$scope.tasks.push(t)
}
else{
alert("Please enter the task to add")
}
}
$scope.editTask=function(task)
{
task.EDITING=true
}
$scope.turnOffEditing=function(task){
task.EDITING=false
}
$scope.deleteTask=function(task)
{
15
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
var index=$scope.tasks.indexOf(task)
$scope.tasks.splice(index,1)
}
});
</script>
</head>
<body ng-app="toDoApp">
<h1>TO DO APPLICATION</h1>
<div ng-controller="toDoAppCntrl">
Enter the name of the Task:
<input type="text" ng-model="newTask">
<button ng-click="addTask()">Add Task</button>
<br/>
<br/>
<table border="1">
<tr>
<th>SLNO</th>
<th>Status</th>
<th>Task</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tr ng-repeat="task in tasks">
<td>{{$index+1}}</td>
<td>
<input type="checkbox" ng-model="task.COMPLETED">
</td>
<td>
<span ng-show="!task.EDITING">{{task.TITLE}}</span>
<input type="text" ng-show="task.EDITING"
ng-model="task.TITLE" ng- blur="turnOffEditing(task)">
</td>
<td>
<button ng-click="editTask(task)">Edit</button>
</td>
<td>
<button ng-click="deleteTask(task)">Delete</button>
</td>
</tr>
</table>
</div>
</body>
</html>
16
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
17
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>USER MANAGEMENT APPLICATION</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("userMgmtApp",[]);
app.controller("userMgmtAppCntrl",function($scope){
$scope.users=[
{'name':"Dr. Harish Kumar BT",
'email':'[email protected]','editing':false},
{'name':'ABC','email':'[email protected]','editing':false},
{'name':'XYZ','email':'[email protected]','editing':false}
]
$scope.createUser=function()
{
$scope.users.push(u)
$scope.newUserName=''
$scope.newUserEmail=''
}
else{
alert("Please provide the user name and email id")
}
$scope.readUser=function(user)
{
user.editing=true
}
18
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
$scope.updateUser=function(user){
user.editing=false
}
$scope.deleteUser=function(user)
{
$scope.users.splice(index,1)
}
}
});
</script>
</head>
<body ng-app="userMgmtApp">
19
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
</td>
<td>
<span ng-show="!user.editing">{{user.email}}</span>
<input type="text" ng-show="user.editing" ng-model="user.email">
</td>
<td>
<button ng-click="readUser(user)">Read</button>
</td>
<td>
<button ng-click="updateUser(user)">Update</button>
</td>
<td>
<button ng-click="deleteUser(user)">Delete</button>
</td>
</tr>
</table>
</div>
</body>
</html>
Output:
20
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
8. Develop AngularJS program to create a login form, with validation for the
username and password fields.
<!DOCTYPE html>
<html>
<title>Angular JS Login Form</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("loginApp",[]);
app.controller('loginAppCntrl',function($scope){
$scope.userName=''
$scope.password=''
$scope.noAttempts=0
$scope.login=function(){
// console.log("Inside login function")
if($scope.userName=="harish" &&
$scope.password=="12345678")
{
alert("Login Successfull")
}
else{
$scope.noAttempts++
if($scope.noAttempts<=3)
{
alert("Incorrect user name/password! Attempt No.
"+$scope.noAttempts)
}
else{
document.getElementById("loginButton").disabled=true
}
}
}
});
</script>
<style>
.error-message{
color:red;
font-size: 20px;
21
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
}
</style>
</head>
</form>
</body>
</html>
22
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
23
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>Angular JS Filter Employee Search Application</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.mi
n.js">
</script>
<script>
var app=angular.module("empSearchApp",[]);
app.controller("empSearchAppCntrl",function($scope){
$scope.empList=[
{'name':'Harish Kumar B T','salary':500000},
{'name':'Chetan','salary':400000},
{'name':'Manju','salary':300000},
{'name':'Prashanth','salary':400000},
{'name':'Thanuja','salary':500000},
{'name':'Manasa','salary':600000}
]
$scope.clearFilters=function()
{
$scope.searchName=''
$scope.searchSalary=''
}
});
</script>
</head>
<body ng-app="empSearchApp">
<h1>Employee Search Application</h1>
<div ng-controller="empSearchAppCntrl">
Search by Employee Name:<input type="text" ng-model="searchName">
Search by Employee salary:<input type="number"
ng-model="searchSalary">
</tr>
</table>
</div>
</body>
</html>
Output:
25
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
26
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>Item Management Application</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("itemMgmtApp",[]);
app.controller("itemMgmtAppCntrl",function($scope){
$scope.itemList=['Pen','Pencil','Eraser','Book']
$scope.addItem=function()
{
if($scope.newItem)
{
if($scope.itemList.indexOf($scope.newItem)==-1)
{
$scope.itemList.push($scope.newItem)
}
else{
alert('This item is already there in the item collection')
}
}
else{
alert('Please Enter the item to add')
}
}
$scope.removeItem=function(item)
{
var yes=confirm("Are you sure you want to delete "+item)
if(yes==true)
{
var index=$scope.itemList.indexOf(item)
$scope.itemList.splice(index,1)
}
}
27
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
});
</script>
</head>
<body ng-app="itemMgmtApp">
<h1>Item Management Application</h1>
<div ng-controller="itemMgmtAppCntrl">
Enter an item to add: <input type="text" ng-model="newItem">
<button ng-click="addItem()">ADD</button>
<br/><br/>
<b>List of Items</b>
<table border="1">
<tr>
<th>SLNO</th>
<th>Item</th>
<th>Remove</th>
</tr>
<tr ng-repeat="item in itemList">
<td>{{$index+1}}</td>
<td>{{item}}</td>
<td><button ng-click=removeItem(item)>Remove</button></td>
</tr>
</table>
<br/>
</body>
</html>
28
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
29
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>Student Details in Uppercase</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("studDetailsUpperApp",[]);
app.controller("studDetailsUpperAppCntrl",function($scope){
$scope.studDetails=['harish','kumar','chetan','prashanth','thanuja']
$scope.upper=true
$scope.lower=false
$scope.Lower=function()
{
//console.log('called')
$scope.upper=false
$scope.lower=true
}
$scope.Upper=function()
{
$scope.upper=true
$scope.lower=false
}
});
</script>
</head>
<body ng-app="studDetailsUpperApp">
<h1>Student Details in Uppercase</h1>
<div ng-controller="studDetailsUpperAppCntrl">
<button ng-click="Upper()">Upper</button>
<button ng-click="Lower()">Lower</button>
<table border="1">
<tr>
<th>SLNO</th>
<th>NAME</th>
30
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
</tr>
<tr ng-repeat="student in studDetails">
<td>{{$index+1}}</td>
<td ng-show="upper">{{student|uppercase}}</td>
<td ng-show="lower">{{student|lowercase}}</td>
</tr>
</table>
</div>
</body>
</html>
Output:
31
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
12.Create an AngularJS application that displays the date by using date filter
parameters.
<!DOCTYPE html>
<html>
<title>Date Application</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("dateApp",[]);
app.controller("dateAppCntrl",function($scope){
$scope.currentDate=new Date();
});
</script>
</head>
<body ng-app="dateApp">
<h1>Date in different formats</h1>
<div ng-controller="dateAppCntrl">
32
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Output:
33
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
Sample Programs
</body>
</html>
</body>
</html>
34
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
3. Example on ng-click
<!DOCTYPE html>
<html>
<title>
Demo on ng-click directive
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.num1=20
$scope.num2=30
$scope.result
$scope.add=function()
{
console.log("Function Called")
$scope.result=$scope.num1 + $scope.num2
// return $scope.result
}
});
</script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCntrl">
Click the button to get the result
<button ng-click="add()">Click Here</button><br/>
Result: {{result}}
</div>
</body>
</html>
35
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
4. Addition program
<!DOCTYPE html>
<html>
<title>
Addition Program
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.num1=0
$scope.num2=0
$scope.result=0
$scope.add=function()
{
$scope.result=$scope.num1 + $scope.num2
}
});
</script>
</head>
<body ng-app="myApp">
<h1>Addition Program</h1>
<div ng-controller="myCntrl">
</div>
</body>
</html>
36
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<html>
<title>Demo on root scope variable</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
>
</script>
<script>
var app=angular.module("myApp",[]);
app.run(function($rootScope){
$rootScope.dept1="CSE"
$rootScope.dept2="ECE"
});
app.controller("myCntrl", function($scope){
$scope.name1="Harish Kumar B T"
});
app.controller("myAnotherCntrl", function($scope){
$scope.name2="Chetan Kumar B T"
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
Dept. of {{dept1}} Faculties<br/>
1. {{name1}}
2. {{name2}}
</div>
<div ng-controller="myAnotherCntrl">
Dept. of {{dept2}} Faculties<br/>
1. {{name1}}
2. {{name2}}
</div>
</body>
</html>
37
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
6. Example on ng-options
<!DOCTYPE html>
<html>
<title> Demo on Using the ng-options to fill the dropdown list from the list
of values</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.myList=['Apple','Mango','Banana','Orange']
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
List of Fruits: <select ng-model="selectFruit" ng-options="fruit for
fruit in myList"></select>
</div>
</body>
</html>
38
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
7. Example on ng-repeat
<!DOCTYPE html>
<html>
<title>
Demo on using ng-repeat
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js
">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.myList=['Cow','Lion','Tiger','Deer','Sheep']
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
<ul>
<li ng-repeat="animal in myList">{{animal}}</li>
</ul>
</div>
</body>
</html>
39
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
8. Example on ng-show
<!DOCTYPE html>
<html>
<title>
Demo pgm on using ng-show directive
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.display=true;
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
<li ng-show="display">Harish</li>
</div>
</body>
</html>
40
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>Random Number Generation Demo</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"
>
</script>
<script>
var app=angular.module("myApp",[]);
app.controller("myCntrl",function($scope){
$scope.randomList=[]
$scope.generateRandomNumbers=function(){
$scope.randomList=[]
for(var i=1;i<=$scope.num;i++)
{
$scope.randomList.push((Math.random()*$scope.num+1).toFixed(2
))
}
}
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="myCntrl">
Enter the Number of random numbers to be generated: <input
type="numbers" ng-model="num">
<button ng-
click="generateRandomNumbers()">Generate</button><br/>
<li ng-repeat="x in randomList">{{x}}</li>
</div>
</body>
</html>
41
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title> Demo on List of Student Objects</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<script>
var app=angular.module("studListApp",[]);
app.controller("studListAppCntrl", function($scope){
$scope.studList=[
{'USN':'1BI20CS001','NAME':'ABC','DEPT':'CSE','SEM':7,'MARKS':78},
{'USN':'1BI20EC002','NAME':'XYZ','DEPT':'ECE','SEM':6,'MARKS':89},
{'USN':'1BI20EE003','NAME':'PQR','DEPT':'EEE','SEM':7,'MARKS':56},
{'USN':'1BI20CS002','NAME':'LMN','DEPT':'CSE','SEM':7,'MARKS':90},
]
});
</script>
</head>
<body ng-app="studListApp">
<h1>Student List</h1>
<div ng-controller="studListAppCntrl">
<table border="1">
<tr>
<th>USN</th>
<th>NAME</th>
<th>DEPT</th>
<th>SEM</th>
<th>MARKS</th>
</tr>
<tr ng-repeat="student in studList">
<td>{{student.USN}}</td>
<td>{{student.NAME}}</td>
<td>{{student.DEPT}}</td>
<td>{{student.SEM}}</td>
<td>{{student.MARKS}}</td>
</tr>
</table>
</div>
</body>
</html>
42
Department of Artificial Intelligence & Data Science, CMRIT
21CSL581 ANGULAR JS Laboratory
<!DOCTYPE html>
<html>
<title>
Demo on ng-blur directive
</title>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
</head>
<body ng-app="">
Keep focus and keep losing focus from me to get the action done:
<input type="text" ng-model="name" ng-init="count=0"
ng-blur="count=count+1">
<br/>
{{count}}
</body>
</html>
43
Department of Artificial Intelligence & Data Science, CMRIT