---
title: "What is a service in AngularJS, and how is it different from a controller?"  
description: "What is a service in AngularJS, and how is it different from a controller?"  
author: "Revati S Misra"  
published: 2023-04-18  
updated: 2023-11-22  
canonical: https://www.mindstick.com/forum/157880/what-is-a-service-in-angularjs-and-how-is-it-different-from-a-controller  
category: "angular js"  
tags: ["angular js", "angularjs development"]  
reading_time: 3 minutes  

---

# What is a service in AngularJS, and how is it different from a controller?

What is a [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) in [AngularJS](https://www.mindstick.com/forum/33926/use-angularjs-without-scope-in-mvc), and how is it different from a [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc)?

## Replies

### Reply by Aryan Kumar

In AngularJS, a service is a singleton object or function that performs a specific task or provides a particular functionality. Services are an essential part of the AngularJS architecture and are designed to promote the modular and reusable structure of the code. They facilitate the sharing of data, logic, and functionality across different parts of an application.

### Characteristics of AngularJS Services:

## Singleton:

- Services are singleton objects, meaning that there is only one instance of a service in an AngularJS application. This single instance is shared across different components, ensuring consistency and a central point for managing state and functionality.

## Dependency Injection:

- AngularJS uses a mechanism called dependency injection to inject services into controllers, directives, filters, and other services. This promotes modularity and loose coupling between different components.

## Reusable:

- Services are designed to be reusable across different parts of an application. They encapsulate a specific piece of functionality, making it easy to use the same service in multiple controllers or other components.

## Separation of Concerns:

- Services help maintain a clear separation of concerns in the AngularJS architecture. Logic related to data manipulation, business rules, or external interactions can be encapsulated in services, keeping controllers lightweight and focused on managing the presentation logic.

## Promotes Code Organization:

- By encapsulating specific functionality in services, the overall structure of an AngularJS application becomes more organized and modular. This makes it easier to manage, maintain, and extend the codebase.

### Example of an AngularJS Service:

```plaintext
// Defining a simple service named 'userService'
angular.module('myApp').service('userService', function() {
    var users = [];

    this.addUser = function(user) {
        users.push(user);
    };

    this.getUsers = function() {
        return users;
    };
});
```

In this example, the **userService** is a simple service that manages a collection of users. It provides methods to add a user to the collection (**addUser**) and retrieve the list of users (**getUsers**).

### Difference Between a Service and a Controller:

## Purpose:

- **Service:** A service is designed to encapsulate and provide a specific piece of functionality or data management. It focuses on performing tasks and sharing data across different components.
- **Controller:** A controller is responsible for managing the presentation logic of a particular view or a section of the UI. It interacts with the DOM, handles user input, and updates the model.

## Instance and Singleton:

- **Service:** Services are singleton objects, meaning there is only one instance of each service in the application.
- **Controller:** Each controller is instantiated for a specific view, creating a new instance for each occurrence of that view.

## Lifetime:

- **Service:** The lifetime of a service extends throughout the duration of the application.
- **Controller:** The lifetime of a controller is tied to the scope of the view it manages. When the scope is destroyed, the controller instance is also destroyed.

## Dependency Injection:

- **Service:** Services can be injected into controllers, other services, directives, and filters using AngularJS's dependency injection mechanism.
- **Controller:** Controllers can receive injected dependencies, including services, through dependency injection.

In summary, while controllers are responsible for managing the presentation logic of a view, services encapsulate reusable functionality and data management, promoting modularity and code organization in AngularJS applications.


---

Original Source: https://www.mindstick.com/forum/157880/what-is-a-service-in-angularjs-and-how-is-it-different-from-a-controller

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
