In this article I will show you how easy it is to consume the WebAPI using AngularJS. To achieve that we are going to create Customer web page where we can do CRUD and filter on List of Customers. Here’s what it look like.
For This webpage we need to create ASP.NET WebAPI and AngularJS script to fetch WebAPI data.
Create Customer ASP.NET WebAPI
We are using WebAPI which I created in this article.
Getting Started with Web Form and WebAPI using Entity Framework 5
AngularJS script
How we fetch WebAPI data?
The $http service is a core Angular service that facilitates communication with the remote HTTP servers. This article we are using $http methods to call WebAPI. You can also use $resource instead on $http.
Define CustomerApp and Factory
var app = angular.module('customerApp', []); var url = 'api/Customers/'; app.factory('customerFactory', function ($http) { return { getCustomer: function () { return $http.get(url); }, addCustomer: function (customer) { return $http.post(url, customer); }, deleteCustomer: function (customer) { return $http.delete(url + customer.CustomerID); }, updateCustomer: function (customer) { return $http.put(url + customer.Id, customer); } }; });
CustomersController
In this controller script we going to use “customerFactory” to do CRUD on Customer Data.
app.controller('CustomersController', function PostsController($scope, customerFactory) { $scope.customers = []; $scope.loading = true; $scope.addMode = false; $scope.toggleEdit = function () { this.customer.editMode = !this.customer.editMode; }; $scope.toggleAdd = function () { $scope.addMode = !$scope.addMode; }; $scope.save = function () { $scope.loading = true; var cust = this.customer; customerFactory.updateCustomer(cust).success(function (data) { alert("Saved Successfully!!"); cust.editMode = false; $scope.loading = false; }).error(function (data) { $scope.error = "An Error has occured while Saving customer! " + data.ExceptionMessage; $scope.loading = false; }); }; // add Customer $scope.add = function () { $scope.loading = true; customerFactory.addCustomer(this.newcustomer).success(function (data) { alert("Added Successfully!!"); $scope.addMode = false; $scope.customers.push(data); $scope.loading = false; }).error(function (data) { $scope.error = "An Error has occured while Adding customer! " + data.ExceptionMessage; $scope.loading = false; }); }; // delete Customer $scope.delcustomer = function () { $scope.loading = true; var currentCustomer = this.customer; customerFactory.deleteCustomer(currentCustomer).success(function (data) { alert("Deleted Successfully!!"); $.each($scope.customers, function (i) { if ($scope.customers[i].CustomerID === currentCustomer.CustomerID) { $scope.customers.splice(i, 1); return false; } }); $scope.loading = false; }).error(function (data) { $scope.error = "An Error has occured while Saving customer! " + data.ExceptionMessage; $scope.loading = false; }); }; //get all Customers customerFactory.getCustomer().success(function (data) { $scope.customers = data; $scope.loading = false; }) .error(function (data) { $scope.error = "An Error has occured while loading posts! " + data.ExceptionMessage; $scope.loading = false; }); });
Full Page Code:
<!DOCTYPE html> <html data-ng-app="customerApp"> <head> <title>Customer App | AshishBlog</title> <script src="http://code.angularjs.org/1.0.6/angular.min.js"></script> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <link type="text/css" href="http://twitter.github.io/bootstrap/assets/css/bootstrap.css" rel="stylesheet" /> <style> #mydiv { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: 1000; background-color: grey; opacity: .8; } .ajax-loader { position: absolute; left: 50%; top: 50%; margin-left: -32px; /* -1 * image width / 2 */ margin-top: -32px; /* -1 * image height / 2 */ display: block; } </style> </head> <body> <div data-ng-controller="CustomersController" class="container"> <h2>Customers</h2> <strong class="error">{{ error }}</strong> <div id="mydiv" data-ng-show="loading"> <img src="Images/482.gif" class="ajax-loader" /> </div> <p data-ng-hide="addMode"><a data-ng-click="toggleAdd()" href="javascript:;" class="btn btn-primary">Add New</a></p> <form name="addCustomer" data-ng-show="addMode" style="width: 600px; margin: 0px auto;"> <label>Name:</label><input type="text" data-ng-model="newcustomer.Name" required /> <label>Email:</label><input type="email" data-ng-model="newcustomer.Email" required /> <br /> <span class="error" data-ng-show="addCustomer.$error.email">Invalid Email format!</span> <br /> <input type="submit" value="Add" data-ng-click="add()" data-ng-disabled="!addCustomer.$valid" class="btn btn-primary" /> <input type="button" value="Cancel" data-ng-click="toggleAdd()" class="btn btn-primary" /> <br /> <br /> </form> <table class="table table-bordered table-hover" style="width: 800px"> <tr> <th>#</th> <th>Name</th> <th>Email</th> <th></th> </tr> <tr> <td></td> <td> <input type="text" data-ng-model="search.Name" /></td> <td> <input type="text" data-ng-model="search.Email" /></td> <td></td> </tr> <tr data-ng-repeat="customer in customers | filter:search"> <td><strong data-ng-hide="customer.editMode">{{ customer.CustomerID }}</strong></td> <td> <p data-ng-hide="customer.editMode">{{ customer.Name }}</p> <p data-ng-show="customer.editMode"> <input type="text" data-ng-model="customer.Name" /> </p> </td> <td> <p data-ng-hide="customer.editMode">{{ customer.Email }}</p> <input data-ng-show="customer.editMode" type="text" data-ng-model="customer.Email" /></td> <td> <p data-ng-hide="customer.editMode"><a data-ng-click="toggleEdit(customer)" href="javascript:;">Edit</a> | <a data-ng-click="delcustomer(customer)" href="javascript:;">Delete</a></p> <p data-ng-show="customer.editMode"><a data-ng-click="save(customer)" href="javascript:;">Save</a> | <a data-ng-click="toggleEdit(customer)" href="javascript:;">Cancel</a></p> </td> </tr> </table> <hr /> </div> <script src="app.js"></script> </body> </html>
Thanks