How to make a RESTful Web Service with AngularJS

The concepts of "Separation of Concerns" for services architecture in the angularjs. Services  are responsible to do a specific tasks only. It can be reutilized as per our requirements.We must create a controller for it. Controllers, filters can call them as on requirement basis.

AngularJS supports a lot of inbuilt services for example, $http, $route, $window, $location etc. Each service is used for a specific task for example, $http is used to make ajax call to get the server data. $route is used to define the routing information and so on. Inbuilt services are always prefixed with $ symbol.

There are two ways to create a service.

  • factory
  • service

Using factory method

Using this method, we first define a factory and then assign method to it. Add this code to the bottom of the JavaScript in the fiddle:

var mainApp = angular.module("mainApp", []);
mainApp.factory('MathService', function() {
   var factory = {};
   factory.multiply = function(a, b) {
      return a * b
   return factory;

Using service method

Using service method, first, we needs to define a service and then assign method to it. We've also injected an already available service to it.

mainApp.service('CalcService', function(MathService){
   this.square = function(a) {
      return MathService.multiply(a,a);


              In this example we needs to find the root of any number which is enter by an user.  There are needs to be include angularjs file then and then only it is working. We also needs to validation the user input, whether it is numeric or not. If not then gives validation error.

                First, We needs to create a html form with all validation. We must required value, length of input and numberic or not that types of validation defines.

               We also needs to includes the angularjs file. Without this js file, it is handle this types of request. Below lines must be includes: angularjs and js controller file. We needs to create a application as webApp. we needs to create model object as webApp. Then make controller name as ApiController and call the object of that conroller as $scope. In that controller call the web services that will created. In that web services we needs to call the function which is also defined with values of user input. In that function the calculation of route value of user input. The codes are given below:


Consider below output:

Let's Think together, Say Something !