Full answer
A service keeps logic that does not belong to one screen, such as loading data, remembering a logged-in user, or keeping a shopping cart. Components stay small and focus on the template.
Dependency injection (DI) means a class does not create its helpers with new. It asks Angular for them. Angular keeps injectors, which are lookup tables from a token (usually the class itself) to an instance. This makes the code easy to test, because a test can supply a fake service.
To make a service, write a class with @Injectable({ providedIn: 'root' }). This registers it with the root injector, so there is one shared instance in the app, and it is removed from the bundle if nobody uses it (tree-shakable).
To use it, call inject(AttendanceService) in a field initializer or in the constructor. This is the modern form. The older form is constructor parameter injection, constructor(private attendance: AttendanceService) {}, which still works and is very common.
inject() only works in an injection context: field initializers, constructors, and factory functions, not in a random method later.
You can also provide a service in a component's providers array. Then each instance of that component gets its own copy of the service. This is useful for state that belongs to one screen.
import { Component, Injectable, computed, inject, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class AttendanceService {
private readonly present = signal<string[]>(['Asha', 'Imran']);
readonly count = computed(() => this.present().length);
readonly names = this.present.asReadonly();
markPresent(name: string): void {
this.present.update((list) => (list.includes(name) ? list : [...list, name]));
}
}
@Component({
selector: 'app-attendance-summary',
template: `
<h2>{{ service.count() }} present</h2>
<button (click)="service.markPresent('Meera')">Mark Meera present</button>
<ul>
@for (n of service.names(); track n) {
<li>{{ n }}</li>
}
</ul>
`,
})
export class AttendanceSummaryComponent {
protected readonly service = inject(AttendanceService);
}Angular 20.3. The service starts with fixed data so the heading always shows the same count.
Key points
- A service holds shared logic and data, and is a plain class with `@Injectable`.
- `providedIn: 'root'` gives one app-wide, tree-shakable instance.
- Use `inject(Service)` in a field or constructor. Constructor parameters are the older, still valid style.
- DI helps testing, because a fake can replace the real service.
Common mistakes
- Creating a service with `new AttendanceService()`. It bypasses the injector and any dependencies it needs.
- Calling `inject()` inside an event handler. It throws outside an injection context.
- Providing the same service both in root and in a component by accident, which creates two separate copies of the data.
Follow-ups
- How can a component get its own private copy of a service?
- What is an injection token?
- How would you replace a service with a fake in a test?
Tips
- Explain DI without jargon: "the component asks, Angular supplies".
- Mention both `inject()` and constructor injection and say which you prefer and why.
- Link services to testing. This shows you understand the purpose, not only the syntax.
Evaluation rubric
- Explains why DI exists.
- Uses providedIn root and inject correctly.
- Knows component-level providers create separate instances.