inject()

Resolves a dependency from the active injector in an injection context.

Since Angular 14+ Spec ↗

Syntax

inject<T>(token: ProviderToken<T>, options?): T

Parameters

NameTypeRequiredDescription
token ProviderToken<T> No Class or InjectionToken to resolve.
options object No optional, self, skipSelf, host modifiers.

Returns

T — The resolved dependency.

Throws

  • NullInjectorError — No provider found and not marked optional.

Examples

import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class TodoService {
  private http = inject(HttpClient);
  list() {
    return this.http.get('/api/todos');
  }
}

Notes

inject() must run in an injection context (constructor, field initializer, factory, or runInInjectionContext). It enables clean composition, base classes without super() plumbing, and functional route guards/resolvers and interceptors.