angular-di

Implement Angular v20+ dependency injection with inject(), tokens, and providers.

2|Updated Jan 5, 2024
One-click install
npx skills add https://github.com/wilfriedago/dotfiles --skill angular-di-wilfriedago
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: angular-di
Source: https://github.com/wilfriedago/dotfiles/tree/main/agents/skills/angular-di
Command: npx skills add https://github.com/wilfriedago/dotfiles --skill angular-di-wilfriedago

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill simplifies and clarifies the implementation of dependency injection (DI) in Angular applications, enabling developers to build more modular, testable, and maintainable codebases.

Core Features & Use Cases

  • Modern DI Practices: Demonstrates the use of inject() for cleaner service and component injection.
  • Provider Scopes: Explains how to manage service lifecycles at root, component, and route levels.
  • Injection Tokens: Covers creating and using injection tokens for custom dependencies and configuration.
  • Advanced Provider Types: Details useClass, useValue, useFactory, and useExisting.
  • DI Patterns: Illustrates patterns like Facade, State, and Repository services.
  • Testing & Cleanup: Shows how to mock services, override providers, and manage resource cleanup with DestroyRef.

Quick Start

Use the angular-di skill to demonstrate how to inject the HttpClient service into an Angular component using the inject() function.

Frequently Asked Questions about angular-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use the inject() function for dependency injection in Angular?

The inject() function provides a modern way to handle Angular dependency injection by replacing constructor-based injection, allowing you to cleanly inject services and components directly within service or component properties.

How do I configure providers to manage singleton vs scoped services in Angular?

Configuring Angular providers at the root level creates singleton services, while defining providers at component or route levels creates scoped services, directly controlling service lifecycles and instance availability.

When should I use injection tokens for custom dependencies in Angular?

Angular injection tokens are needed when passing custom dependencies or configuration objects that lack a concrete class, allowing you to define injectable interfaces and decouple consumers from specific implementations.

What's the difference between useClass, useValue, useFactory, and useExisting providers?

Angular provider types differentiate dependency instantiation: useClass generates instances, useValue supplies static data, useFactory creates dependencies dynamically, and useExisting aliases existing providers to share instances.

How do I mock services and override providers when testing Angular dependency injection?

Angular dependency injection testing involves overriding providers in the test configuration to supply mock services, ensuring components are evaluated against isolated test data rather than real service implementations.

Can I manage resource cleanup in Angular services using DestroyRef?

DestroyRef enables resource cleanup in Angular dependency injection by registering callbacks that trigger when an injector is destroyed, safely managing subscriptions and releasing resources outside component lifecycles.