Introduction

In this post, we will learn how we can use AngularJS Framework with Web API2 to call HTTP Services implemented in Part 1. I hope, you will like this.

In this part, we are going to-

Implement module, routing, service and controller

First of all, we need to add 3 JS files respectively- AppEmployee.js, EmployeeService.js and EmployeeController.js

controller

For doing this, right click on AppEmployee folder> Add > JavaScript file.

controller

AppEmployee.js

  1. var app = angular.module('myApp', ['ngRoute']);
  2. app.config(['$routeProvider', function($routeProvider) {
  3. $routeProvider.when('/EmployeeGrid', { //route to get EmployeeGrid.html page
  4. templateUrl: '/AppEmployee/Views/EmployeeGrid.html',
  5. controller: 'EmployeeController'
  6. }).when('/AddEmployee', { //route to get AddEmployee.html page
  7. templateUrl: '/AppEmployee/Views/AddEmployee.html',
  8. controller: 'EmployeeController'
  9. }).when('/EditEmployee/:Id', { //route to get UpdateEmployee.html page
  10. templateUrl: '/AppEmployee/Views/UpdateEmployee.html',
  11. controller: 'EmployeeController'
  12. }).when('/DeleteEmployee/:Id', { //route to get DeleteEmployee.html page
  13. templateUrl: '/AppEmployee/Views/DeleteEmployee.html',
  14. controller: 'EmployeeController'
  15. }).otherwise({
  16. controller: 'EmployeeController'
  17. });
  18. }]);
  19. // This is just a sample script. Paste your real code (javascript or HTML) here.
  20. if ('this_is' == /an_example/) {
  21. of_beautifier();
  22. } else {
  23. var a = b ? (c % d) : e[f];
  24. }
In order to ensure routing, we have injected ngRoute module, which gives access to use $routeProvider for configuring routes, as shown above.

Once configuring routes are finished, it’s time to implement all the Services required, which should be used to perform CRUD operations.

EmployeeService.js
  1. app.factory('EmployeeFactory', function($http, $q, $timeout) {
  2. var factory = {
  3. EmployeeList: [],
  4. //Get Employee List
  5. getEmployeeList: function() {
  6. var deferred = $q.defer();
  7. $http.get('api/Employee/GetListEmployee').success(function(data) {
  8. factory.EmployeeList = data;
  9. $timeout(function() {
  10. deferred.resolve(factory.EmployeeList);
  11. }, 2000);
  12. }).error(function() {
  13. deferred.reject('Error !');
  14. });
  15. return deferred.promise;
  16. },
  17. //Add New Employee
  18. addNewEmployee: function(newEmployee) {
  19. var deferred = $q.defer();
  20. $http.post('api/Employee/AddNewEmployee', newEmployee).success(function(data) {
  21. deferred.resolve(newEmployee);
  22. }).error(function() {
  23. deferred.reject('Error !');
  24. });
  25. return deferred.promise;
  26. },
  27. //Details Employee
  28. detailsEmployee: function(id) {
  29. var deferred = $q.defer();
  30. $http.get('api/Employee/DetailsEmployee/' + id).success(function(data) {
  31. $timeout(function() {
  32. deferred.resolve(data);
  33. }, 2000);
  34. }).error(function() {
  35. deferred.reject('Error !');
  36. });
  37. return deferred.promise;
  38. },
  39. //Update Employee
  40. updateEmployee: function(Employee) {
  41. var deferred = $q.defer();
  42. $http.put('api/Employee/UpdateEmployee', Employee).success(function(data) {
  43. deferred.resolve(data);
  44. }).error(function() {
  45. deferred.reject('Error !');
  46. });
  47. return deferred.promise;
  48. },
  49. //Delete Employee
  50. deleteEmployee: function(id) {
  51. var deferred = $q.defer();
  52. $http.delete('api/Employee/DeleteEmployee/' + id).success(function(data) {
  53. deferred.resolve(data);
  54. }).error(function() {
  55. deferred.reject('Error !');
  56. });
  57. return deferred.promise;
  58. }
  59. };
  60. return factory
  61. });
For implementing services, I have chosen Factory but you can also use Service or provider to achieve our requirement.

Function Description
getEmployeeList() It Is used to retrieve all the data from the database table.
addNewEmployee() It Is defined to add new employee in our database.
detailsEmployee(id) Performs to get back data related to each employee. This function takes an Id as a parameter.
updateEmployee() It Is used to modify information’s employee.
deleteEmployee(id) This function deletes an employee, based on his Id.

Note

EmployeeController.js

  1. app.controller('EmployeeController', ['$scope', 'EmployeeFactory', '$location', '$routeParams', function($scope, EmployeeFactory, $location, $routeParams) {
  2. $scope.EmployeeList;
  3. //Get all Employee
  4. EmployeeFactory.getEmployeeList().then(function(data) {
  5. $scope.EmployeeList = data;
  6. }, function(msg) {
  7. console.log(msg);
  8. });
  9. //Add new Employee
  10. $scope.AddNewEmployee = function() {
  11. var employee = {
  12. FirstName: $scope.FirstName,
  13. LastName: $scope.LastName,
  14. Gender: $scope.Gender,
  15. Designation: $scope.Designation,
  16. Salary: $scope.Salary,
  17. City: $scope.City,
  18. Country: $scope.Country
  19. };
  20. //debugger;
  21. EmployeeFactory.addNewEmployee(employee).then(function(data) {
  22. $location.path('/EmployeeGrid');
  23. }, function() {
  24. console.log('Error');
  25. });
  26. };
  27. //mapping Employee
  28. function mappingEmployee(employee) {
  29. $scope.EmployeeID = employee.EmployeeID;
  30. $scope.FirstName = employee.FirstName;
  31. $scope.LastName = employee.LastName;
  32. $scope.Gender = employee.Gender;
  33. $scope.Designation = employee.Designation;
  34. $scope.Salary = employee.Salary;
  35. $scope.City = employee.City;
  36. $scope.Country = employee.Country;
  37. }
  38. if ($routeParams.Id) {
  39. var id = $routeParams.Id;
  40. EmployeeFactory.detailsEmployee(id).then(function(data) {
  41. mappingEmployee(data);
  42. }, function() {
  43. console.log('Error');
  44. });
  45. }
  46. //Update Employee
  47. $scope.UpdateEmployee = function() {
  48. var employee = {
  49. EmployeeID: $scope.EmployeeID,
  50. FirstName: $scope.FirstName,
  51. LastName: $scope.LastName,
  52. Gender: $scope.Gender,
  53. Designation: $scope.Designation,
  54. Salary: $scope.Salary,
  55. City: $scope.City,
  56. Country: $scope.Country
  57. };
  58. //debugger;
  59. EmployeeFactory.updateEmployee(employee).then(function(data) {
  60. $location.path('/EmployeeGrid');
  61. }, function() {
  62. console.log('Error');
  63. });
  64. }
  65. //Delete Employee
  66. $scope.DeleteEmployee = function() {
  67. var Id = $scope.EmployeeID;
  68. EmployeeFactory.deleteEmployee(Id).then(function(data) {
  69. $location.path('/EmployeeGrid');
  70. }, function(msg) {
  71. console.log(msg);
  72. })
  73. }
  74. }]);
Now, we are going to inject our factory in Employee controller which allow us to call different services implemented as explained previously.

In Employee controller, I have used the same logic based on 2 steps,

Note

Don’t forget to call the following libraries inside Layout.cshtml

Layout cshtml

Create different html pages to perform CRUD operations

Now, add new folder from Solution Explorer and inside it, we are going to add 4 HTML files.

Layout cshtml

EmployeeGrid.html

  1. <ahrefahref="#/AddEmployee" class="btn btn-default btn-sm">Add New Employee</a>
  2. <br/>
  3. <divclassdivclass="table-responsive">
  4. <tableidtableid="mytable" class="table table-bordred table-striped">
  5. <thead>
  6. <th>EmployeeID</th>
  7. <th>First Name</th>
  8. <th>Last Name</th>
  9. <th>Gender</th>
  10. <th>Designation</th>
  11. <th>Salary</th>
  12. <th>City</th>
  13. <th>Country</th>
  14. <th>Edit</th>
  15. <th>Delete</th>
  16. </thead>
  17. <tbody>
  18. <trng-repeattrng-repeat="iteminEmployeeList">
  19. <td>{{item.EmployeeID}}</td>
  20. <td>{{item.FirstName}}</td>
  21. <td>{{item.LastName}}</td>
  22. <td>{{item.Gender}}</td>
  23. <td>{{item.Designation}}</td>
  24. <td>{{item.Salary}}</td>
  25. <td>{{item.City}}</td>
  26. <td>{{item.Country}}</td>
  27. <td>
  28. <aclassaclass="btn btn-primary btn-success" href="#/EditEmployee/{{item.EmployeeID}}">
  29. Edit
  30. </a>
  31. </td>
  32. <td>
  33. <aclassaclass="btn btn-warning" href="#/DeleteEmployee/{{item.EmployeeID}}">
  34. Delete
  35. </a>
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </div>
In EmployeeGrid page, we have defined a simple table, using bootstrap, which displays all the data from the database table. To fetch data , we need to use ng-repeatdirective.

Now, you can run your Application. Don’t forget to change the URL address, given below-

http://localhost:55192/#/EmployeeGrid


Let’s see the output, given below-

<ahref="#/AddEmployee" class="btn btn-default btn-sm" />Add New Employee</a> <br/>
<divclass="table-responsive">
<tableid="mytable" class=
  1. EmployeeID First Name Last Name Gender Designation Salary City Country Edit Delete {
  2. {
  3. item.EmployeeID
  4. }
  5. } {
  6. {
  7. item.FirstName
  8. }
  9. } {
  10. {
  11. item.LastName
  12. }
  13. } {
  14. {
  15. item.Gender
  16. }
  17. } {
  18. {
  19. item.Designation
  20. }
  21. } {
  22. {
  23. item.Salary
  24. }
  25. } {
  26. {
  27. item.City
  28. }
  29. } {
  30. {
  31. item.Country
  32. }
  33. }
  34. Edit Delete ">
AddEmployee.html
  1. <formmethodformmethod="post">
  2. <h4> Add New Employee </h4>
  3. <divclassdivclass="form-group">
  4. <labelforlabelfor="InputFirstName">FirstName</label>
  5. <inputtypeinputtype="text" class="form-control" id="InputFirstName" placeholder="First Name" ng-model="FirstName">
  6. </div>
  7. <divclassdivclass="form-group">
  8. <labelforlabelfor="InputLastName">Last Name</label>
  9. <inputtypeinputtype="text" class="form-control" id="InputLastName" placeholder="Last Name" ng-model="LastName">
  10. </div>
  11. <divclassdivclass="form-group">
  12. <labelforlabelfor="InputGender">Gender</label>
  13. <inputtypeinputtype="text" class="form-control" id="InputGender" placeholder="Gender" ng-model="Gender">
  14. </div>
  15. <divclassdivclass="form-group">
  16. <labelforlabelfor="InputDesignation">Designation</label>
  17. <inputtypeinputtype="text" class="form-control" id="InputDesignation" placeholder="Designation" ng-model="Designation">
  18. </div>
  19. <divclassdivclass="form-group">
  20. <labelforlabelfor="InputSalary">Salary</label>
  21. <inputtypeinputtype="text" class="form-control" id="InputSalary" placeholder="Salary" ng-model="Salary">
  22. </div>
  23. <divclassdivclass="form-group">
  24. <labelforlabelfor="InputCity">City</label>
  25. <inputtypeinputtype="text" class="form-control" id="InputCity" placeholder="City" ng-model="City">
  26. </div>
  27. <divclassdivclass="form-group">
  28. <labelforlabelfor="InputCountry">Country</label>
  29. <inputtypeinputtype="text" class="form-control" id="InputCountry" placeholder="Country" ng-model="Country">
  30. </div>
  31. <buttontypebuttontype="submit" class="btn btn-primary" ng-click="AddNewEmployee()">Save Employee</button>
  32. </form>
Here, we have defined a Form with all the required fields to add a new employee.

Let’s see the output, given below-

output
UpdateEmployee.html
  1. <formmethodformmethod="post">
  2. <h4> Update Employee </h4>
  3. <divclassdivclass="form-group">
  4. <labelforlabelfor="InputFirstName">FirstName</label>
  5. <inputtypeinputtype="text" class="form-control" id="InputFirstName" placeholder="First Name" ng-model="FirstName">
  6. </div>
  7. <divclassdivclass="form-group">
  8. <labelforlabelfor="InputLastName">Last Name</label>
  9. <inputtypeinputtype="text" class="form-control" id="InputLastName" placeholder="Last Name" ng-model="LastName">
  10. </div>
  11. <divclassdivclass="form-group">
  12. <labelforlabelfor="InputGender">Gender</label>
  13. <inputtypeinputtype="text" class="form-control" id="InputGender" placeholder="Gender" ng-model="Gender">
  14. </div>
  15. <divclassdivclass="form-group">
  16. <labelforlabelfor="InputDesignation">Designation</label>
  17. <inputtypeinputtype="text" class="form-control" id="InputDesignation" placeholder="Designation" ng-model="Designation">
  18. </div>
  19. <divclassdivclass="form-group">
  20. <labelforlabelfor="InputSalary">Salary</label>
  21. <inputtypeinputtype="text" class="form-control" id="InputSalary" placeholder="Salary" ng-model="Salary">
  22. </div>
  23. <divclassdivclass="form-group">
  24. <labelforlabelfor="InputCity">City</label>
  25. <inputtypeinputtype="text" class="form-control" id="InputCity" placeholder="City" ng-model="City">
  26. </div>
  27. <divclassdivclass="form-group">
  28. <labelforlabelfor="InputCountry">Country</label>
  29. <inputtypeinputtype="text" class="form-control" id="InputCountry" placeholder="Country" ng-model="Country">
  30. </div>
  31. <buttontypebuttontype="submit" class="btn btn-primary" ng-click="UpdateEmployee()">Update Employee</button>
  32. </form>
Let’s see the output, given below-


<formmethod="post" />
<h4> Update Employee </h4>
<divclass="form-group"> <labelfor="inputfirstname">FirstName</label> <inputtype="text" class=
Last Name Gender Designation Salary City Country Update Employee ">

DeleteEmployee.html
  1. <divclassdivclass="panel panel-default">
  2. <divclassdivclass="panel-heading">Delete Employee Panel</div>
  3. <divclassdivclass="panel-body">
  4. <div>Do you want to delete <b>{{FirstName}}</b> ??</div><br/><br/>
  5. <buttontypebuttontype="submit" class="btn btn-info" ng-click="DeleteEmployee()">
  6. <spanclassspanclass="glyphicon glyphicon-ok-sign">
  7. </span> Delete Employee </button>
  8. </div>
  9. </div>
Let’s see the output, given below-

output
Happy coding.

That’s all, Please send your feedback and queries in comments box.