Create Angular JS App

Now it is time to create angular js [Single Page Application] in shore SPA. Here we will be using API service to perform CRUD operations using angular js. Create a folder named with "App" and inside this folder create two JS files like "app.js" and "EmployeeController.js".

You need to create Views folder inside the App folder and create 4 html template files for CRUD operations as following image.

folder

App.Js

As we know, an Angular JS application runs inside the ng-app directive. So, the first step is to create routing for angular js app.
  1. var app = angular.module('myApp', ['ngRoute']);
  2. app.config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) {
  3. $routeProvider.when('/EmployeeList', { //Routing for show list of employee
  4. templateUrl: '/App/Views/EmployeeList.html',
  5. controller: 'EmployeeController'
  6. }).when('/AddEmployee', { //Routing for add employee
  7. templateUrl: '/App/Views/AddEmployee.html',
  8. controller: 'EmployeeController'
  9. })
  10. .when('/EditEmployee/:empId', { //Routing for geting single employee details
  11. templateUrl: '/App/Views/EditEmployee.html',
  12. controller: 'EmployeeController'
  13. })
  14. .when('/DeleteEmployee/:empId', { //Routing for delete employee
  15. templateUrl: '/App/Views/DeleteEmployee.html',
  16. controller: 'EmployeeController'
  17. })
  18. .otherwise({ //Default Routing
  19. controller: 'EmployeeController'
  20. })
  21. }]);
EmployeeController.js

Now to perform CRUD operations on client side, we need to add EmployeeController.js file which will hanlde all the CRUD opertions with http services.
  1. app.controller("EmployeeController", ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {
  2. $scope.ListOfEmployee;
  3. $scope.Status;
  4. $scope.Close = function () {
  5. $location.path('/EmployeeList');
  6. }
  7. //Get all employee and bind with html table
  8. $http.get("api/employee/GetAllEmployee").success(function (data) {
  9. $scope.ListOfEmployee = data;
  10. })
  11. .error(function (data) {
  12. $scope.Status = "data not found";
  13. });
  14. //Add new employee
  15. $scope.Add = function () {
  16. var employeeData = {
  17. FirstName: $scope.FirstName,
  18. LastName: $scope.LastName,
  19. Address: $scope.Address,
  20. Salary: $scope.Salary,
  21. DOB: $scope.DOB,
  22. // DepartmentID: $scope.DepartmentID
  23. };
  24. debugger;
  25. $http.post("api/employee/AddEmployee", employeeData).success(function (data) {
  26. $location.path('/EmployeeList');
  27. }).error(function (data) {
  28. console.log(data);
  29. $scope.error = "Something wrong when adding new employee " + data.ExceptionMessage;
  30. });
  31. }
  32. //Fill the employee records for update
  33. if ($routeParams.empId) {
  34. $scope.Id = $routeParams.empId;
  35. $http.get('api/employee/GetEmployee/' + $scope.Id).success(function (data) {
  36. $scope.FirstName = data.FirstName;
  37. $scope.LastName = data.LastName;
  38. $scope.Address = data.Address;
  39. $scope.Salary = data.Salary;
  40. $scope.DOB = data.DOB
  41. //$scope.DepartmentID = data.DepartmentID
  42. });
  43. }
  44. //Update the employee records
  45. $scope.Update = function () {
  46. debugger;
  47. var employeeData = {
  48. EmployeeID: $scope.Id,
  49. FirstName: $scope.FirstName,
  50. LastName: $scope.LastName,
  51. Address: $scope.Address,
  52. Salary: $scope.Salary,
  53. DOB: $scope.DOB
  54. //DepartmentID: $scope.DepartmentID
  55. };
  56. if ($scope.Id > 0) {
  57. $http.put("api/employee/UpdateEmployee", employeeData).success(function (data) {
  58. $location.path('/EmployeeList');
  59. }).error(function (data) {
  60. console.log(data);
  61. $scope.error = "Something wrong when adding updating employee " + data.ExceptionMessage;
  62. });
  63. }
  64. }
  65. //Delete the selected employee from the list
  66. $scope.Delete = function () {
  67. if ($scope.Id > 0) {
  68. $http.delete("api/employee/DeleteEmployee/" + $scope.Id).success(function (data) {
  69. $location.path('/EmployeeList');
  70. }).error(function (data) {
  71. console.log(data);
  72. $scope.error = "Something wrong when adding Deleting employee " + data.ExceptionMessage;
  73. });
  74. }
  75. }
  76. }]);
So, we have done mostly everything besides creating template file. So, first I will create the template file for showing the list of employees.

Employee List

To see the employee list, we need to create EmployeeList.html page.
  1. <a href="#/AddEmployee" class="btn btn-primary btn-xs" >Add New Employee</a>
  2. <br /><br />
  3. <div class="table-responsive">
  4. <table id="mytable" class="table table-bordred table-striped">
  5. <thead>
  6. <th>ID</th>
  7. <th>First Name</th>
  8. <th>Last Name</th>
  9. <th>Address</th>
  10. <th>Salary</th>
  11. <th>DOB</th>
  12. <!--<th>Department</th>-->
  13. <th>Edit</th>
  14. <th>Delete</th>
  15. </thead>
  16. <tbody>
  17. <tr ng-repeat="item in ListOfEmployee">
  18. <td>{{item.EmployeeID}}</td>
  19. <td>{{item.FirstName}}</td>
  20. <td>{{item.LastName}}</td>
  21. <td>{{item.Address}}</td>
  22. <td>{{item.Salary}}</td>
  23. <td>{{item.DOB}}</td>
  24. <!--<td>{{item.DepartmentName}}</td>-->
  25. <td><a class="btn btn-primary btn-xs" href="#/EditEmployee/{{item.EmployeeID}}">
  26. <span class="glyphicon glyphicon-pencil"></span></a></td>
  27. <td><a class="btn btn-danger btn-xs" href="#/DeleteEmployee/{{item.EmployeeID}}">
  28. <span class="glyphicon glyphicon-trash"></span></a></td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
Now it is time to run the project and see how data will be displayed with angular js app. To run the application, just press F5 and change the url as "http://localhost:56983/#/EmployeeList". When application will run, output will be like the below image.

application

Add Employee

To add new employe with Angular JS application, we just need to create one more template page with name"AddEmployee.html" as following.
  1. <div id="add" tabindex="-1" role="dialog" aria-labelledby="add-modal-label">
  2. <div class="modal-dialog" role="document">
  3. <div class="modal-content">
  4. <form class="form-horizontal" id="add-form">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
  7. <h4 class="modal-title" id="add-modal-label">Add new Employee</h4>
  8. </div>
  9. <div class="modal-body">
  10. <div class="form-group">
  11. <label for="add-firstname" class="col-sm-2 control-label">Firstname</label>
  12. <div class="col-sm-10">
  13. <input type="text" class="form-control" id="add-firstname" name="firstname" placeholder="Firstname" ng-model="FirstName" required>
  14. </div>
  15. </div>
  16. <div class="form-group">
  17. <label for="add-lastname" class="col-sm-2 control-label">LastName</label>
  18. <div class="col-sm-10">
  19. <input type="text" class="form-control" id="add-lastname" name="lastName" placeholder="LastName" ng-model="LastName" required>
  20. </div>
  21. </div>
  22. <div class="form-group">
  23. <label for="add-address" class="col-sm-2 control-label">Address</label>
  24. <div class="col-sm-10">
  25. <input type="text" class="form-control" id="add-address" name="address" placeholder="Address" ng-model="Address">
  26. </div>
  27. </div>
  28. <div class="form-group">
  29. <label for="add-salary" class="col-sm-2 control-label">Salary</label>
  30. <div class="col-sm-10">
  31. <input type="text" class="form-control" id="add-salary" name="mobile" placeholder="Salary" ng-model="Salary" required>
  32. </div>
  33. </div>
  34. <div class="form-group">
  35. <label for="add-dob" class="col-sm-2 control-label">DOB</label>
  36. <div class="col-sm-10">
  37. <input type="text" class="form-control" id="add-dob" name="mobile" placeholder="DOB" ng-model="DOB" required> [ex: 1990-01-21]
  38. </div>
  39. </div>
  40. </div>
  41. <div class="modal-footer">
  42. <button type="button" class="btn btn-default" data-dismiss="modal" ng-click="Close()">Close</button>
  43. <button type="submit" class="btn btn-primary" ng-click="Add()">Save changes</button>
  44. </div>
  45. </form>
  46. </div>
  47. </div>
  48. </div>
Now just click on the "Add New Employee" button or change the url as like"http://localhost:56983/#/AddEmployee". It will show the page with some control where you can enter the required details for employee. When you click on Save changes then it will add the new employee.

add the new employee

Update Employee

From the employee list page, there is button to edit/update the employee list. To click on any, you can update the respected details for that employee. Create the EditEmployee.html page as following.
  1. <div id="edit" aria-labelledby="edit" aria-hidden="true">
  2. <div class="modal-dialog">
  3. <div class="modal-content">
  4. <form class="form-horizontal" id="edit-form">
  5. <div class="modal-header">
  6. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
  7. <h4 class="modal-title" id="edit-modal-label">Update Employee</h4>
  8. </div>
  9. <div class="modal-body">
  10. <div class="form-group">
  11. <label for="edit-firstname" class="col-sm-2 control-label">Firstname</label>
  12. <div class="col-sm-10">
  13. <input type="text" class="form-control" id="edit-firstname" name="firstname" placeholder="Firstname" ng-model="FirstName" required>
  14. </div>
  15. </div>
  16. <div class="form-group">
  17. <label for="edit-lastname" class="col-sm-2 control-label">LastName</label>
  18. <div class="col-sm-10">
  19. <input type="text" class="form-control" id="edit-lastname" name="lastName" placeholder="LastName" ng-model="LastName">
  20. </div>
  21. </div>
  22. <div class="form-group">
  23. <label for="edit-address" class="col-sm-2 control-label">Address</label>
  24. <div class="col-sm-10">
  25. <input type="text" class="form-control" id="edit-address" name="address" placeholder="Address" ng-model="Address">
  26. </div>
  27. </div>
  28. <div class="form-group">
  29. <label for="edit-salary" class="col-sm-2 control-label">Salary</label>
  30. <div class="col-sm-10">
  31. <input type="text" class="form-control" id="edit-salary" name="mobile" placeholder="Salary" ng-model="Salary">
  32. </div>
  33. </div>
  34. <div class="form-group">
  35. <label for="edit-dob" class="col-sm-2 control-label">DOB</label>
  36. <div class="col-sm-10">
  37. <input type="text" class="form-control" id="edit-dob" name="mobile" placeholder="DOB" ng-model="DOB">
  38. </div>
  39. </div>
  40. </div>
  41. <div class="modal-footer">
  42. <button type="button" class="btn btn-default" ng-click="Close()">Close</button>
  43. <button type="submit" class="btn btn-primary" ng-click="Update()">Update</button>
  44. </div>
  45. </form>
  46. </div>
  47. <!-- /.modal-content -->
  48. </div>
  49. <!-- /.modal-dialog -->
  50. </div>
and result will be displayed as like following,

Update Employee

Delete Employee

To delete the employee, just click on delete button to delete respective employee. I have createdDeleteEmployee.html page.
  1. <div id="delete" ng-controller="EmployeeController">
  2. <div class="modal-dialog">
  3. <div class="modal-content">
  4. <div class="modal-header">
  5. <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
  6. <h4 class="modal-title custom_align" id="Heading">Delete this entry</h4>
  7. </div>
  8. <div class="modal-body">
  9. <div class="alert alert-danger"><span class="glyphicon glyphicon-warning-sign"></span> Are you sure you want to delete Record for <b>{{FirstName + " "+LastName}}</b>?</div>
  10. </div>
  11. <div class="modal-footer ">
  12. <button type="submit" class="btn btn-success" ng-click="Delete()"><span class="glyphicon glyphicon-ok-sign"></span> Yes</button>
  13. <button type="button" class="btn btn-default" ng-click="Close()"><span class="glyphicon glyphicon-remove"></span> No</button>
  14. </div>
  15. </div>
  16. <!-- /.modal-content -->
  17. </div>
  18. <!-- /.modal-dialog -->
  19. </div>
When you will click on delete button, it will open a page where it will ask for confirmation for delete.

Delete Employee

Conclusion

So, today we have learned how to create a Web API and how to create an Angular JS and perform CRUD operations.

I hope this post will help you. Please put your feedback using comments which helps me to improve myself for the next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends.