Dependency injection fundamentals and the injector tree
What problem dependency injection solves, and the two ways to ask Angular's injector for something.
Dependency injection fundamentals and the injector tree
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Dependency injection means a class declares what it needs and lets something else — the injector — supply it, instead of constructing its own dependencies with `new`.
- `@Injectable()` marks a class as something the injector knows how to construct and hand out; constructor parameters are how a class declares what it needs.
- `inject()` gets you the same thing a constructor parameter does, callable anywhere function-injection-context allows it — not only inside a constructor.
The problem with a class building its own dependencies
class UserService {
private http = new HttpClient(); // constructing its own dependency
}
This looks harmless, but it quietly commits UserService to one specific,
concrete HttpClient, built right there, every single time a UserService gets
constructed. Want to substitute a fake HTTP client for a test? You can’t, not
without changing UserService’s own source. Want a different configuration of
HttpClient in one part of the app? Same problem. The class has tightly welded
itself to exactly how its dependency gets built, and that decision is baked in at
the one place least convenient to change it — inside the class that needs it,
not whoever’s using it.
Declare what you need, let something else supply it
@Injectable({ providedIn: 'root' })
class UserService {
constructor(private http: HttpClient) {}
}
Flip the responsibility: UserService declares, through its constructor
parameter, “I need something that is an HttpClient.” It doesn’t construct one.
Something else — Angular’s injector — is responsible for actually producing
that HttpClient instance and handing it over when UserService gets
constructed. That something-else is what makes substitution possible: a test can
configure the injector to hand UserService a fake HttpClient instead,
without changing a single line of UserService itself, because UserService
never specified how to get one — only that it needed one.
@Injectable() is what marks a class as something the injector is allowed to
construct and manage this way. providedIn: 'root' tells Angular where in the
injector hierarchy this particular service should live — the next lesson covers
exactly what that hierarchy looks like and what your other options are.
inject(): the same request, as a function call
class UserService {
private http = inject(HttpClient);
}
This asks for exactly the same thing the constructor parameter did — “give me an
HttpClient” — just expressed as a function call instead of a constructor
parameter. The practical difference is where you’re allowed to use it: a
constructor parameter only exists inside a constructor’s parameter list;
inject() works anywhere Angular considers you to be in an “injection context” —
field initializers (as above), factory functions passed to provideX() calls,
and a few other places a constructor parameter simply can’t reach. Neither form
is “more correct” than the other for ordinary cases; inject() tends to win out
specifically in the places a constructor parameter can’t go.
The injector isn’t one global bucket
“Something else supplies the dependency” raises an obvious follow-up: where does that something-else actually keep its supply of things to hand out, and is there only one? There isn’t — Angular’s injectors form a tree, mirroring your component tree, and different services can live at different levels of it.
What happens when nothing provides what’s requested
It’s worth knowing what failure actually looks like here, since it’s a
message you’ll meet constantly while learning Angular: asking for a token with
no matching provider anywhere in the injector tree throws
NullInjectorError: No provider for X! at the moment the injector tries (and
fails) to resolve the request — not a silently undefined value. That’s a
deliberate design choice: a missing dependency is almost never something code
downstream can sensibly continue without, so Angular fails loudly and
immediately, pointing you straight at the token name that couldn’t be
resolved, rather than letting a null propagate quietly into code that
assumed it would never see one.
This lesson’s video walks through the core request-and-supply relationship with a working example; the linked “deeper” video on hierarchical injectors is the direct next step once this mechanism is solid, since the next two lessons build on exactly this foundation.
Check yourself
If `UserService` directly did `this.http = new HttpClient()` inside its own constructor, what would break the moment you tried to unit-test it?
You'd have no way to substitute a fake `HttpClient` for testing — the real one would always get constructed, making every test that touches `UserService` an accidental integration test against a real network call. Dependency injection avoids this because the injector decides what to hand over, and a test can configure the injector to hand over a mock instead.
What's the difference between `constructor(private http: HttpClient) {}` and `private http = inject(HttpClient);` as ways to get the same dependency?
Functionally the same result — both ask the injector for an `HttpClient` instance. The constructor form ties the request to a parameter; `inject()` is a plain function call that works in more places (field initializers, factory functions, anywhere within an injection context), not only inside a constructor's parameter list.
Go deeper
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.