Tevpro insights

Angular inject() vs. Constructor Injection: When to Use Each

Compare Angular inject() with constructor injection, see where an injection context is required, and decide when a migration makes sense.

Software Development

Key takeaways

  • Angular supports both inject() and constructor injection; choose the pattern that fits the class or functional API.
  • inject() works in an Angular injection context, such as a field initializer or provider factory, not in an arbitrary method.
  • Test behavior after running the Angular inject() migration schematic, especially in existing code with inheritance or optional dependencies.

Angular supports both constructor injection and the inject() function. inject() can declare a dependency in a class field or an Angular injection context, such as a provider factory. Constructors remain valid; use inject() where it clarifies dependencies or fits a functional API. Calling it outside an injection context throws an error. For the official migration steps, see Angular’s inject() function migration, as well as the full API reference for Angular's inject() API.

The Traditional Constructor Pattern

Constructor injection is still supported. The common pattern looks like this:

Code example
angular-ts
constructor(private abc: AbcService) {}

While this works well, constructors can quickly become crowded when injecting multiple services, and the pattern doesn't translate well to functional or factory-based code. Developers often found themselves adding unused constructor parameters temporarily or reorganizing constructor signatures to keep the code readable.

The New inject() Class Field Approach

Angular's new convention allows developers to declare dependencies directly inside the class without a constructor:

Code example
angular-ts
private readonly abc = inject(AbcService);

This approach reduces boilerplate and makes dependencies easier to locate and manage. inject() can also be called in supported injection contexts, including provider factories and functional guards. It cannot be called from an arbitrary method or after the instance has been created. It also supports refactoring, since developers can add or remove services without modifying constructor signatures or breaking inheritance patterns.

Key Advantages of the inject() Pattern

Using inject() provides several benefits:

  • Cleaner, constructor-free components
  • Better readability with explicit, per-field injection
  • Works in functional and factory APIs
  • Simplifies unit testing and mocking
  • Aligns with Angular's standalone architecture
  • Makes adding new dependencies less intrusive

Before running the inject() migration

Angular provides ng generate @angular/core:inject for converting constructor-injected dependencies. Run it on a limited path first, review the diff, and run your compile and tests. Abstract classes are not migrated by default because their parameters may not be injectable. Inheritance can need backwardsCompatibleConstructors, and optional dependencies may become nullable in the generated types.

Angular CLI migration
bash
ng generate @angular/core:inject

inject() needs a current injection context, such as a field initializer, constructor, provider factory, or functional guard. Do not move a call into a later click handler or arbitrary class method. A working constructor is still a valid choice; migrate because the new pattern helps the code, not solely for uniformity.

Final Thoughts

Use inject() where field-level dependencies or a functional API make the code clearer. Constructor injection remains supported. If you use the migration schematic, review inheritance, optional types, and the runtime injection context before accepting the change.

Sources

  1. [1] Angular inject() migrationangular.dev
  2. [2] Angular injection contextangular.dev

Written by Kim Pham

Why work with us

Why Tevpro?

Whether you’re a startup with a bold product idea or an established company seeking a stronger delivery partner, Tevpro delivers results. Our expert consultants specialize in building secure, scalable applications that simplify operations and drive real ROI.

FAQ

Questions about Angular inject()

No. Constructor injection remains supported. Use inject() where a field initializer or functional API makes the dependency clearer; a working constructor does not need to be changed just for style.