What is an Angular service and how is the @Injectable decorator used?
Learn what an Angular service is and how the @Injectable decorator wires it into components through dependency injection, with examples and interview tips.
Expected Interview Answer
An Angular service is a reusable class that holds business logic, data access, or shared state, and @Injectable marks it so Angular's dependency injection system can create and supply it to components and other services.
Services keep components lean by moving non-UI concerns like HTTP calls, logging, and state out of the component. The @Injectable() decorator, typically with providedIn: 'root', registers the class with the injector so Angular can instantiate it once and inject the same singleton wherever it is requested through a constructor parameter.
- Separates business logic from UI components
- Enables code reuse across the application
- Provides singleton shared state via the root injector
- Makes classes easy to unit test with mock dependencies
- Supports tree-shaking when using providedIn: 'root'
AI Mentor Explanation
A service is like the team physio shared by every player: batters and bowlers all request treatment from the same specialist instead of each carrying their own. @Injectable is the team registering that physio on the official staff list so anyone on the field can call for them on demand.
Step-by-Step Explanation
Step 1
Create the class
Define a plain TypeScript class that holds the shared logic, such as a DataService with HTTP methods.
Step 2
Add @Injectable
Decorate the class with @Injectable({ providedIn: 'root' }) so Angular's injector can construct it.
Step 3
Declare dependencies
List any collaborators (like HttpClient) as constructor parameters so they are injected in turn.
Step 4
Inject into a component
Add the service as a constructor parameter in the component; Angular supplies the singleton automatically.
Step 5
Use the service
Call the service's methods from the component to fetch data or run logic, keeping the component focused on the view.
What Interviewer Expects
- Clear definition of a service as reusable, non-UI logic
- Understanding of dependency injection and injectors
- Knowledge of providedIn: 'root' and singletons
- How constructor injection wires services into components
- Awareness of tree-shakable providers
Common Mistakes
- Thinking @Injectable is required on every class rather than injectable ones
- Confusing a service with a component
- Manually instantiating a service with new instead of injecting it
- Forgetting providedIn and then not registering the provider anywhere
Best Answer (HR Friendly)
“An Angular service is a shared helper class that holds logic like fetching data, so components stay focused on displaying it. The @Injectable label lets Angular automatically create the service and hand it to whichever components ask for it.”
Code Example
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class UserService {
constructor(private http: HttpClient) {}
getUsers() {
return this.http.get('/api/users');
}
}import { Component } from '@angular/core';
import { UserService } from './user.service';
@Component({ selector: 'app-users', template: '' })
export class UsersComponent {
constructor(private userService: UserService) {}
load() {
this.userService.getUsers().subscribe(console.log);
}
}Follow-up Questions
- What is the difference between providedIn: 'root' and registering a service in a module's providers array?
- How do you provide a service scoped to a single component?
- How does Angular's hierarchical injector resolve dependencies?
- How would you mock a service in a unit test?
MCQ Practice
1. What does @Injectable({ providedIn: 'root' }) do?
providedIn: 'root' registers a tree-shakable, application-wide singleton in the root injector.
2. How is a service typically supplied to a component?
Angular injects services by matching constructor parameters against registered providers.
3. What is the main purpose of a service?
Services encapsulate business logic, data access, and shared state so components stay focused on the view.
Flash Cards
What is an Angular service? — A reusable class holding business logic, data access, or shared state, injected into components via DI.
What does @Injectable do? — Marks a class so Angular's dependency injection system can create and supply it.
What does providedIn: 'root' create? — A tree-shakable, application-wide singleton in the root injector.
How is a service used in a component? — Declared as a constructor parameter; Angular injects the instance automatically.