Services as shared state
Using a singleton service as the place two unrelated components meet, instead of routing everything through inputs and outputs.
Services as shared state
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Two components with no parent-child relationship can't communicate through @Input()/@Output() at all — a shared, injected service is the standard way to connect them.
- A `providedIn: 'root'` service is a singleton: every component that injects it gets the exact same instance, which is what makes it a valid place to hold state two components both need to see.
- Reach for a shared service when state needs to outlive, or exist outside of, any single component's lifetime — not as the default home for every piece of state in the app.
Components that have no relationship still need to talk
@Input()/@Output() (and their signal-based equivalents) solve exactly one
shape of communication: a parent and its direct child. That covers a lot of real
cases, but plenty of components that need to coordinate have no such
relationship at all — a notification icon in the page header and a task list
three levels deep in an entirely different part of the component tree share no
parent-child link whatsoever. There’s no binding syntax that reaches across that
gap, because @Input()/@Output() was never designed to.
A shared singleton as the meeting point
@Injectable({ providedIn: 'root' })
export class NotificationService {
private count = signal(0);
readonly unreadCount = this.count.asReadonly();
increment() {
this.count.update((n) => n + 1);
}
clear() {
this.count.set(0);
}
}
// NotificationBellComponent
notifications = inject(NotificationService);
// template: {{ notifications.unreadCount() }}
// TaskListComponent, somewhere unrelated in the tree
notifications = inject(NotificationService);
onTaskCompleted() {
this.notifications.increment();
}
Both components inject the same token, and because the service is
providedIn: 'root', both get the exact same instance — the one the root
injector constructed the first time anything asked for it. There’s no wiring
between the two components directly; each one only knows about the service. When
TaskListComponent calls .increment(), it’s mutating a signal that
NotificationBellComponent’s template is already reading, so the bell updates
without either component needing to know the other exists.
This lesson’s video builds exactly this kind of cross-component communication pattern directly, using signals inside the shared service the same way the example above does — worth watching for how little code is actually needed once the service itself holds the state, compared to routing the same data through a chain of inputs and outputs across several intermediate components that don’t otherwise care about it.
When this is the right tool, and when it isn’t
A shared service earns its place specifically when state needs to be visible to
components that aren’t in a direct parent-child relationship, or needs to
outlive any single component’s own lifetime (surviving navigation away and back,
for instance). It’s not a default replacement for @Input()/@Output() — a
straightforward parent-child relationship is still better served by passing data
directly, which keeps the data flow visible in the template rather than hidden
inside a service only code-reading reveals. Reach for a shared service when the
components genuinely have no other way to meet, not as a shortcut around writing
a few bindings.
A note on testing a service used this way
Because NotificationService is an ordinary injectable class with no special
coupling to either component, testing the interaction doesn’t require
rendering either one. A unit test can inject the service directly (the same
TestBed.inject() pattern a later lesson on testing covers in full), call
.increment(), and assert on .unreadCount() — verifying the shared-state
behavior itself, completely independent of whichever components happen to
consume it.
Where this is headed
This pattern — a service holding reactive state via signals — is the seed of real state management. A few lessons from now, this roadmap covers exactly when “a plain service holding a signal” stops being enough and something like NgRx earns its complexity instead.
Check yourself
A `NotificationBellComponent` in the page header needs to show a count that a completely unrelated `TaskListComponent` elsewhere on the page updates. Why won't @Input()/@Output() solve this?
@Input()/@Output() only connect a parent to its direct children — they require one component to literally contain the other in the template tree. `NotificationBellComponent` and `TaskListComponent` have no such relationship, so there's no binding syntax available between them at all; they need a shared intermediary instead, which is exactly what an injected service provides.
If `NotificationService` is `providedIn: 'root'`, and both `NotificationBellComponent` and `TaskListComponent` inject it, are they getting two separate instances or one shared one?
One shared instance. `providedIn: 'root'` means the root injector constructs exactly one instance of the service for the whole app, and hands that same instance to every component that injects it — which is precisely why updating state on it from one component is visible to another.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.