Angular Services: Your App's Shared Toolbox

An Angular service is a shared toolbox for your app. Instead of components building their own tools (like an HTTP client), they request a single instance from the dependency injector.
Why it exists
As applications grow, you need to share functionality like fetching data or logging across many components. Creating this logic inside each component leads to code duplication and makes the app hard to maintain, test, and scale. Services solve this by centralizing shared code in one place.
The mental model
An Angular service is a shared toolbox managed by Angular's Dependency Injection (DI) system. A component doesn't build its own tools; it simply declares what it needs in its constructor (e.g., "I need a logger"). The DI system then provides the single, shared instance of that tool (the service) to any component that asks for it.
How it works
You create a standard TypeScript class and add the @Injectable() decorator. To make it a singleton available everywhere, you use @Injectable({ providedIn: 'root' }). This tells Angular to create one instance of the service for the entire application. Then, in any component, you "inject" it by adding it as a typed parameter in the constructor, like constructor(private myService: MyService) {}. Angular handles the rest.
When to use it
Services are the backbone of most Angular applications. Use them for tasks that need to be shared or are independent of a specific view. Common uses include: first, making API requests to a server (data clients); second, managing user authentication; third, sharing application state between unrelated components; and fourth, centralizing logging or error handling.
When not to use it
Avoid putting logic that is tightly coupled to a single component's view or internal state into a service. If the logic or data is only ever used by one component and its direct children, it's often simpler to manage it within the component itself or pass it down with @Input() properties.
One canonical example
A logging service prevents console.log calls from being scattered everywhere. First, define the service: import { Injectable } from '@angular/core'; @Injectable({ providedIn: 'root' }) export class AnalyticsLogger { trackEvent(category: string, value: string) { console.log('Analytics event:', { category, value }); } }. Then, to use it, a component injects it in its constructor: constructor(private logger: AnalyticsLogger) { this.logger.trackEvent('page_view', 'home'); }.
Interview question
What is the main advantage of using an Angular Service for tasks like data fetching or logging?
- a.To centralize shared functionality, preventing code duplication and improving maintainability.Correct
- b.To encapsulate logic tightly coupled to a single component's view for better reusability.
- c.To allow components to directly access and modify each other's private properties.
- d.To execute long-running operations in a separate process, ensuring the UI remains responsive.
Why? this is the answer
Services centralize shared functionality like data fetching or logging, which prevents code duplication and makes the application easier to maintain and test. Option B is incorrect because the card explicitly states that services should NOT be used for logic tightly coupled to a single component's view.
Just read this? Test yourself on what you have been reading.
Read the original → angular.dev
- #angular
- #dependency injection
- #services
- #architecture
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.
See open roles