Services And Custom Services In AngularJS

Overview

In this article, we will see what services and custom services are in AngularJS. First, we will learn about services in Angular and why we use services in Angular JS.

For more articles on AngularJS

Introduction: In this article, we will see:

  • What is a service in Angular.
  • Why do we need a service in Angular application.
  • What are the benefits of using services.

What is a service in Angular?

Before we see what a service is in Angular, we will discuss about a normal development scenario. Suppose, you have developed a web service, aka WCF services, like an object. You can map that object in any service. Similarly, a service in Angular is an object that we can map in our Angular services.

Angular JS has lots of built-in services. We have already seen the $http and $log services in our previous article. 

$http service is used for making the AJAX calls while the $log service is used for calling an object of a console, which is very useful in debugging applications. We can also build our custom services in AngularJS.

Thus, a service in AngularJS is simply an object that provides some sort of service which can be reused further in other applications.

Why do we need services?

The primary duty of Controller is to build the Model for the View. E.g., to display the list of employee details in ascending order or descending order and so on. In general, the Controller should not be doing much operations. In our previous examples, we have used $http an $log in our Controller.

Services encapsulate the reusable logic that doesn't belong to anywhere i.e. Directives, Filters, Models and Controllers.

What are the benefits of using Services?

  • Reusability
    The main logic behind any webservice is that it should be reusable through an application and other applications too. Suppose, you want to make an AJAX call, you can use a built-in AngularJS Service $http, simply by injecting it into the object that needs the service.

  • Dependency Injection
    They can be simply injected into Controllers or other services which need them.

  • Testablity
    Since services are used in Controllers, it becomes very easy to test them. You can pass the real implementation and perform the unit testing also.

Custom Services in Angular

We will first see how to create a custom service in Angular. Now, here in our page, I have taken two textboxes and one button, as shown below:

  1. <table>  
  2.     <tr>  
  3.         <td>ResultOne</td>  
  4.         <td><input type="text" /></td>  
  5.     </tr>  
  6.     <tr>  
  7.         <td>ResultTwo</td>  
  8.         <td><input type="text" /></td>  
  9.     </tr>  
  10.     <tr>  
  11.         <td> <input type="button" value="procees" /> </td>  
  12.     </tr>  
  13. </table>  
So, when you reload the page, you will get two textboxes and one button. Here, you can type a string and convert that string. So, we need to add some code. In our Model, first we will add ng-model on two input actions and ng-click on the button a function to perform those operations.
  1. <td><input type="text" ng-model="input" /></td>  
  2. <td><input type="text" ng-model="output" /></td>  
  3. <input type="button" ng-click="transform(input)" value="procees" />  
So, my final page code in display.html is:
  1. <body ng-app="mymodule">  
  2.     <div ng-controller="myController">  
  3.         <table>  
  4.             <tr>  
  5.                 <td>ResultOne</td>  
  6.                 <td><input type="text" ng-model="input" /></td>  
  7.             </tr>  
  8.             <tr>  
  9.                 <td>ResultTwo</td>  
  10.                 <td><input type="text" ng-model="output" /></td>  
  11.             </tr>  
  12.             <tr>  
  13.                 <td> <input type="button" ng-click="transform(input)" value="procees" /> </td>  
  14.             </tr>  
  15.         </table>  
  16.     </div>  
  17. </body>  
Now, we will go back to our js file.

Now, we need to create a function in which we need to perform those operations where we need to validate the input string store in a variable and process the output.
  1. var mypartone = angular.module("mymodule", []).controller("myController"function($scope) {  
  2.     $scope.transform = function(input) {  
  3.         if (!input) {  
  4.             return input;  
  5.         }  
  6.         var output = "";  
  7.         for (var i = 0; i < input.length; i++) {  
  8.             if (i > 0 && input[i] == input[i].toUpperCase()) {  
  9.                 output = output + " ";  
  10.             }  
  11.             output = output + input[i];  
  12.         }  
  13.         $scope.output = output;  
  14.     }  
  15. });  
Here, I have created a function named as transform and attached it to $scope object. Now, that input function will return a string and will be storing those in a variable.

So, when you reload the page, you will get this output:

output

As you can see from the code, it has becomes tedious. We will add a JS file and will reference those in our main script file, as shown below:

Add a new JS file in your solution.

new js file

Name it as StringTest and just add the reference of previous script file and paste it, as shown below.
  1. /// <reference path="mytest.js" />  
  2. app.factory('string'function() {  
  3.     return {  
  4.         transform: function(input) {  
  5.             if (!input) {  
  6.                 return input;  
  7.             }  
  8.             var output = "";  
  9.             for (var i = 0; i < input.length; i++) {  
  10.                 if (i > 0 && input[i] == input[i].toUpperCase()) {  
  11.                     output = output + " ";  
  12.                 }  
  13.                 output = output + input[i];  
  14.             }  
  15.             return output;  
  16.         }  
  17.     };  
  18. });  
Now, just go back to your main controller and add the function name.
  1. var mypartone = angular.module("mymodule", []).controller("myController"function($scope, string) {  
  2.     $scope.transform = function(input) {  
  3.         $scope.output = string.transform(input);  
  4.     }  
  5. });  
Just add reference to your html page, as,
  1. <script src="Scripts/StringService.js"></script>  
Now, reload the page. You will get the same output.

output

Conclusion

So, this was about services and creating custom services in AngularJS. Hope this article was helpful !!

 

Up Next
    Ebook Download
    View all
    Learn
    View all