Write an angular JS application to access JSON file data of an employee from a server using
$http service.
Create Folder and files like the below with commands
1 create a file employees.json
[
{
"id": 101,
"name": "Alice Johnson",
"designation": "Software Engineer",
"salary": 70000
},
{
"id": 102,
"name": "Bob Smith",
"designation": "Project Manager",
"salary": 90000
},
{
"id": 103,
"name": "Charlie Williams",
"designation": "UI Designer",
"salary": 60000
}
]
2 Create a file app.js
// Define the AngularJS app
var app = angular.module('EmployeeApp', []);
// Define the controller
app.controller('EmployeeController', function($scope, $http) {
// Fetch data using $http GET method
$http.get('employees.json')
.then(function(response) {
$scope.employees = response.data; // assign data to scope variable
})
.catch(function(error) {
console.error("Error loading employee data:", error);
});
});
3 create a file index.html
<!DOCTYPE html>
<html ng-app="EmployeeApp">
<head>
<meta charset="UTF-8">
<title>Employee Data</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="EmployeeController">
<h2>Employee List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Designation</th>
<th>Salary</th>
</tr>
<tr ng-repeat="emp in employees">
<td>{{ emp.id }}</td>
<td>{{ emp.name }}</td>
<td>{{ emp.designation }}</td>
<td>{{ emp.salary | currency }}</td>
</tr>
</table>
</body>
</html>
Run the http-server with the following command
Open the browser and give any of the following url