CHAPTER 09
Beginner
Angular Services and Dependency Injection
Updated: May 18, 2026
5 min read
# CHAPTER 9
Angular Services and Dependency Injection
1. Chapter Introduction
Components should only handle the UI — displaying data and responding to user actions. Fetching data from APIs, managing application state, or calculating business logic inside a component creates "fat components" that are impossible to test and reuse. Services are Angular's solution. A Service is a class that handles data logic. Dependency Injection (DI) is Angular's system for automatically providing a Service to any component that needs it.2. Learning Objectives
- Understand why Services are needed.
- Create a service using the Angular CLI.
-
Understand the
@Injectabledecorator.
- Inject a service into a component.
- Share state between multiple components via a singleton service.
3. Creating a Service
bash
This generates task.service.ts:
typescript
4. Dependency Injection: Using the Service
To use a service, you inject it through the component's constructor. Angular's DI system automatically creates the instance!
typescript
html
5. Singleton: Shared State Between Components
BecauseprovidedIn: 'root' creates a single shared instance, two different components injecting the same service share the same data!
text
All three components look at the same TaskService instance. When TaskFormComponent adds a task, TaskListComponent and TaskCountComponent automatically reflect the update.
6. Mini Project: Task Manager Service
Complete working task manager using a service to coordinate two components.
task-form.component.ts
task-count.component.ts
7. Common Mistakes
-
Creating service instances with
new: Never writelet service = new TaskService()in a component. This bypasses the DI system and creates a separate, unshared instance that defeats the Singleton pattern.
-
Business logic in components: If you write API calls directly in
ngOnInitof a component without a service, that component becomes impossible to test and reuse.
8. Best Practices
-
One service per responsibility: Create
AuthServicefor auth,ProductServicefor products. Don't put all logic in one giant service.
- Make services pure: Services should not depend on Angular's UI or DOM. This keeps them testable with simple unit tests.
9. MCQs with Answers
Question 1
What is the primary purpose of an Angular Service?
Question 2
What decorator marks a class as an Angular Service?
Question 3
What does providedIn: 'root' mean?
Question 4
How do you inject a service into a component?
Question 5
Why should you never manually instantiate a service using new MyService()?
providedIn: 'root'), do they share the same data?
a) No, each gets its own copy b) Yes, they share the same Singleton instance
Answer: b) Yes, they share the same Singleton.
Question 7
What Angular CLI command generates a service named auth?
Question 8
Which lifecycle hook is best for calling a service to load initial data?
Question 9
What is Dependency Injection?
10. Interview Questions
- Q: What is the Singleton Pattern in Angular Services? Why is it useful?
- Q: Explain Dependency Injection in Angular. How does the framework know which service to provide to which component?