@Injectable

Marks a class as available to Angular's dependency injection system.

Since Angular 2+ Spec ↗

Syntax

@Injectable(config?: { providedIn })

Parameters

NameTypeRequiredDescription
config object No Optional; providedIn: 'root' registers a tree-shakable singleton.

Returns

ClassDecorator — Decorator enabling DI for the class.

Examples

import { Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class UserService {
  getUsers() {
    return fetch('/api/users').then((r) => r.json());
  }
}

Notes

`providedIn: 'root'` creates an application-wide singleton that is tree-shakable when unused. The decorator is also required whenever a class has injected dependencies of its own. Inject the service via the inject() function or constructor parameters.