---
title: "AngularJs Services"  
description: "Hi everyone in this blog I’m explaining about AngularJs Services.Introduction:AngularJS supports the concepts of \"Separation of Concerns\" using servic"  
author: "Anonymous User"  
published: 2015-03-25  
updated: 2018-02-22  
canonical: https://www.mindstick.com/blog/10862/angularjs-services  
category: "angular js"  
tags: ["angularjs service", "angularjs factory", "angularjs model"]  
reading_time: 2 minutes  

---

# AngularJs Services

Hi everyone in this blog I’m explaining about [AngularJs](https://www.mindstick.com/articles/13081/advantages-disadvantages-of-angularjs-is-that-ideal-for-your-project) Services.

##### Introduction:

##

AngularJS supports the concepts of "Separation of Concerns" using services architecture. Services are [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript) [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) and are [responsible](https://answers.mindstick.com/qa/94149/who-is-one-of-the-responsible-person-for-the-department-of-space-dos) to do a specific task only. This [makes them](https://answers.mindstick.com/qa/40905/the-declaration-speaks-of-truths-which-are-self-evident-what-are-these-truths-why-are-they-called-truths-what-makes-them-self-evident) an individual entity which is maintainable and testable. [Controllers](https://www.mindstick.com/forum/155773/how-asynchronous-controllers-work-in-asp-dot-net-mvc), filters can call them as on [requirement](https://yourviews.mindstick.com/view/85169/becoming-an-influencer-requirement-of-skills-and-knowledge) basis. Services are normally injected using [dependency injection](https://www.mindstick.com/articles/335832/services-and-dependency-injection-in-angularjs) mechanism of AngularJS.

AngularJS provides many in build services for example, $http, $route, $window, $location etc. Each service is responsible 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 to create a service.

- v Factory
- v Service

##### Using Factory Method:

##

Using factory method, we first define a factory and then assign method to it.

```
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 we define a service and then assign method to it. We have also injected an already available service to it.

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

##### Example:

##

Following example will showcase all the above mentioned directives.

```
<html><head>    <title>Angular JS Services</title>    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script></head><body>    <h2>AngularJS Sample Application</h2>    <div ng-app="mainApp" ng-controller="CalcController">        <p>            Enter a number:            <input type="number" ng-model="number" />            <button ng-click="square()">X<sup>2</sup></button>        <p>Result: {{result}}</p>    </div>    <script>        var mainApp = angular.module("mainApp", []);        mainApp.factory('MathService', function () {            var factory = {};            factory.multiply = function (a, b) {                return a * b            }            return factory;        });         mainApp.service('CalcService', function (MathService) {            this.square = function (a) {                return MathService.multiply(a, a);            }        });         mainApp.controller('CalcController', function ($scope, CalcService) {            $scope.square = function () {                $scope.result = CalcService.square($scope.number);            }        });    </script></body></html>
```

##### Output:

##

![AngularJs Services](https://www.mindstick.com/blogs/ef9d0098-8194-4476-a46e-e911bef12e29/images/69a2a7fc-6f40-4236-86d7-5d577c238871.png)

---

Original Source: https://www.mindstick.com/blog/10862/angularjs-services

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
