The inject Function
Resolve dependencies with the inject function.
The inject Function is a free Angular Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The inject Function
The inject() function retrieves a dependency from the injector. It is the modern alternative to constructor parameter injection.
Basic Usage
Call inject with the class you want, and assign the result to a field.
import { Component, inject } from '@angular/core';
import { DataService } from './data.service';
@Component({ selector: 'app-x', template: '' })
export class XComponent {
private data = inject(DataService);
}Compared to Constructor Injection
The older style declares dependencies as constructor parameters. inject() does the same job as a field initializer, which is often cleaner.
// old style:
// constructor(private data: DataService) {}Where inject Can Be Called
inject() must run in an injection context: during construction of a component, directive, service, or in field initializers and provider factory functions.
Injecting in a Service
Use inject inside services too, for example to get the HttpClient.
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({ providedIn: 'root' })
export class Api {
private http = inject(HttpClient);
}Less Boilerplate
With inject you avoid long constructor signatures. Each dependency is a simple field declaration, easy to read and reorder.
Using Injected Services
Once injected, call the service like any object.
export class XComponent {
private store = inject(CounterStore);
add() { this.store.increment(); }
}inject in Functional Guards
Functional route guards and resolvers run in an injection context, so you can call inject inside them to access services.
// export const authGuard = () => {
// const auth = inject(AuthService);
// return auth.isLoggedIn();
// };Optional Dependencies
inject accepts options, such as marking a dependency optional so it returns null instead of throwing when not provided.
// const logger = inject(Logger, { optional: true });Outside Injection Context
Calling inject outside an injection context (for example in a setTimeout callback) throws an error. Capture the dependency during construction instead.
Why Prefer inject
inject works well with mixins, base classes, and functional APIs, and keeps component classes concise. It is the recommended approach in modern Angular.
Quick Check: inject
Retrieving a dependency.
Recap: inject
You learned the inject() function as the modern way to obtain dependencies, usable in components, services, and functional guards within an injection context. It reduces boilerplate versus constructor injection and supports options like optional. Next: providers and scopes.
Frequently asked questions
Is the “The inject Function” lesson free?
Yes — the full text of “The inject Function” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.
What will I learn in “The inject Function”?
Resolve dependencies with the inject function. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Angular Academy?
No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The inject Function” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Angular Academy lesson?
Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Services and Injectable
- The inject Function
- Providers and Scopes
- Injection Tokens