---
title: "How do you create and use a custom service in AngularJS?"  
description: "How do you create and use a custom service in AngularJS?"  
author: "Ravi Vishwakarma"  
published: 2025-03-30  
updated: 2025-04-01  
canonical: https://www.mindstick.com/forum/161389/how-do-you-create-and-use-a-custom-service-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js"]  
reading_time: 2 minutes  

---

# How do you create and use a custom service in AngularJS?

How do you create and use a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) in AngularJS?

## Replies

### Reply by Khushi Singh

An AngularJS custom service functions as a storage unit which allows developers to share logic throughout controllers and directives together with other services. Services support code structuring through modular architecture because they carry out data operations and manage business rules or interface with external application programming interfaces.

There are three options for creating custom services, including `.service()`, .factory(), and .provider(). The .service() method serves as the standard methodology to create constructor functions that define services in AngularJS.

The definition of custom services should occur inside AngularJS modules and their subsequent injection needs to happen in controller definitions. This service handles user login operations in the following way:

```javascript
angular.module('myApp', [])
 .service('AuthService', function() {
   this.isAuthenticated = false;
   this.login = function(username, password) {
     if (username === 'admin' && password === '1234') {
       this.isAuthenticated = true;
       return true;
     }
     return false;
   };
   this.logout = function() {
     this.isAuthenticated = false;
   };
   this.getAuthStatus = function() {
     return this.isAuthenticated ? 'Authenticated' : 'Not Authenticated';
   };
 })
 .controller('LoginController', function($scope, AuthService) {
   $scope.login = function() {
     $scope.status = AuthService.login($scope.username, $scope.password) ? 'Login Successful' : 'Invalid Credentials';
   };
   $scope.logout = function() {
     AuthService.logout();
     $scope.status = 'Logged Out';
   };
 });
```

AuthService provides the functions to manage both login and logout sequences along with authentication management. From within the LoginController the injected service receives usage to execute its methods. Using this structure allows AngularJS applications to achieve greater reusability as well as maintainability because logic exists independently from the controller.


---

Original Source: https://www.mindstick.com/forum/161389/how-do-you-create-and-use-a-custom-service-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
