Sub topic 1
What dependency injection gives you in Angular
Prerequisites
This chapter assumes you know TypeScript classes and decorators, and how to build a standalone Angular component. You should also have used a service in a component before. Routing configuration appears only briefly here.
The problem DI solves
A component often needs help from other classes. It might need an HTTP client, a logger, or a shared counter. If the component creates these helpers with new, it controls how they are built. That causes three problems:
- Two components that call
new CounterService()get two separate counters, even when you want them to share one. - A test cannot swap in a fake, because the real class is written into the component.
- Changing how a helper is built means editing every place that builds it.
Dependency injection (DI) moves that job out of the component. The component states what it needs. Angular finds or creates the object and hands it over. The object it hands over is called a dependency, and the part of Angular that supplies it is an injector.
A first service
A service is a plain class that Angular can provide. This one holds a counter as a signal.
import { Injectable, signal } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CounterService {
readonly count = signal(0);
increment(): void {
this.count.update(n => n + 1);
}
}providedIn: 'root' registers the service with the root environment injector. The whole application shares one instance, created the first time something asks for it. If nothing ever asks, Angular's build tools can leave the class out of the bundle. This is called being tree-shakable.
Asking for a dependency
A component asks for a service with inject(). The class itself is the token, which is the key Angular uses to find the right provider.
import { Component, inject } from '@angular/core';
import { CounterService } from './counter.service';
@Component({
selector: 'app-counter',
standalone: true,
template: `<button (click)="counter.increment()">{{ counter.count() }}</button>`,
})
export class CounterComponent {
protected readonly counter = inject(CounterService);
}The component never writes new. It receives whichever CounterService the injector chooses. If you place two CounterComponent elements on a page, both buttons show the same count, because both receive the root instance.
Why "hierarchical" matters
Angular does not keep one big list of services. It keeps a tree of injectors. A request starts at the injector closest to the component and moves up until some injector has a provider for the token. Where you register a provider therefore decides who shares an instance. The next chapters cover this in detail.
What you get
| Benefit | How DI provides it |
|---|---|
| Sharing | One provider location gives one shared instance to everything below it. |
| Isolation | A provider closer to a component gives that component its own instance. |
| Testability | Tests register a fake under the same token. |
| Smaller bundles | Unused root services can be removed at build time. |
Exercise
Two instances of CounterComponent appear on one page, and CounterService uses providedIn: 'root'. When one button is clicked, what happens to the other button's number, and why?
Show answer
Both numbers change. Both components ask the same injector chain for CounterService, and the root injector holds the only instance, so they share one count signal.