0Pricing
Angular Academy · Lesson

The inject Function

Resolve dependencies with the inject function.

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);
}

All lessons in this course

  1. Services and Injectable
  2. The inject Function
  3. Providers and Scopes
  4. Injection Tokens
← Back to Angular Academy