Overview
The@Injectable decorator marks a class as available to be provided and injected as a dependency. It enables Angular’s dependency injection system to provide instances of the class where needed.
Signature
Parameters
Injectable
Optional configuration object that determines how and where the service is provided.
Injectable Options
providedIn
'root' | 'platform' | null
Determines which injector provides the injectable.
'root'- Provided in the root injector (application-level, singleton)'platform'- Provided in the platform injector (shared across apps)null- Not automatically provided, must be added to aprovidersarray
The
providedIn: 'any' option is deprecated. Use 'root' instead.Basic Service Example
user.service.ts
Service with Dependencies
data.service.ts
Using Services in Components
app.component.ts
Component-Level Providers
notification.service.ts
notification.component.ts
When provided in a component, each instance of the component gets its own instance of the service.
Factory Providers
logger.service.ts
app.config.ts
Class Providers with useClass
config.service.ts
app.config.ts
Value Providers
constants.ts
app.config.ts
Inject Function
modern.service.ts
Service Lifecycle
cleanup.service.ts
Testing Services
user.service.spec.ts
Provider Scopes
Best Practices
Related APIs
- inject() - Modern dependency injection function
- InjectionToken - Create custom injection tokens
- Injector - Manual service instantiation
- @Component - Component decorator with providers
See Also
Dependency Injection
Learn DI fundamentals
Hierarchical Injectors
Understand injector hierarchy
Creating Services
Service creation guide
Providers
Configure providers
