angular-di

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

Updated Aug 10, 2025
One-click install
npx skills add https://github.com/afonsoft/ai-studio-workspace --skill angular-di-afonsoft
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: angular-di
Source: https://github.com/afonsoft/ai-studio-workspace/tree/main/agents/skills/angular-di
Command: npx skills add https://github.com/afonsoft/ai-studio-workspace --skill angular-di-afonsoft

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Configuring dependency injection in modern Angular requires choosing between inject() and constructor injection, selecting correct provider scopes, and wiring injection tokens correctly. This Skill provides concrete patterns for service architecture, provider configuration, and DI hierarchy in Angular v20+ applications. ## Core Features & Use Cases - Modern Injection Patterns: Use inject() instead of constructor injection, create injectable services with providedIn, and manage singleton versus component-scoped instances. - Injection Tokens & Provider Types: Create InjectionToken objects with factories, and configure useClass, useValue, useFactory, useExisting, and multi providers. - Advanced Scenarios: App initializers, environment injectors, runInInjectionContext, hierarchical injection, testing with mocked providers, and DestroyRef cleanup. - Use Case: When building a feature that needs a route-scoped service shared across child components, use route-level providers and inject() to share one instance without prop drilling. ## Quick Start Ask the AI to create an Angular service using inject() with an injection token for the API URL and configure it in app.config.ts.

Frequently Asked Questions about angular-di

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

FAQPage Schema
How do I use inject() instead of constructor injection in Angular?▼

Call inject() as a property initializer inside a component or service, such as private http = inject(HttpClient). It works in injection contexts like constructors, field initializers, and factory functions, and is the preferred pattern in Angular v20+.

How do I create an injection token in Angular?▼

Create a token with new InjectionToken<Type>('description') and optionally add providedIn and a factory for self-providing tokens. Provide values with { provide: TOKEN, useValue: value } in app.config.ts, then consume them with inject(TOKEN).

What is the difference between providers and viewProviders in Angular?▼

providers makes services available to the component and both its view and content children, while viewProviders restricts availability to the component and its view children only. Content children projected via ng-content cannot access viewProviders.

How do I mock services when testing Angular components?▼

Create a spy object with jasmine.createSpyObj and override the provider in TestBed using { provide: ServiceName, useValue: spy }. You can also use TestBed.overrideProvider to replace injection token values for specific test scenarios.

When should I use component-level providers instead of providedIn root?▼

Use component-level providers when each component instance needs its own service instance, such as per-editor state. Use providedIn: 'root' for singletons shared application-wide like authentication or logging services.