angular-di

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

Updated Jun 28, 2026
One-click install
npx skills add https://github.com/412181-HerediaLara/ScaffoldingBE-FE --skill angular-di-412181-heredialara
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: angular-di
Source: https://github.com/412181-HerediaLara/ScaffoldingBE-FE/tree/main/FE/.agents/skills/angular-di
Command: npx skills add https://github.com/412181-HerediaLara/ScaffoldingBE-FE --skill angular-di-412181-heredialara

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Configuring Angular dependency injection correctly—choosing provider scopes, creating injection tokens, and managing service lifecycles—is error-prone without clear patterns, leading to duplicated instances, circular dependencies, and untestable code. ## Core Features & Use Cases - Modern inject() API: Replace constructor injection with the inject() function for services, components, and tokens. - Provider Configuration: Configure root, component, and route-level providers using useClass, useValue, useFactory, and useExisting. - Injection Tokens & Patterns: Create typed InjectionTokens, multi-providers, app initializers, and advanced patterns like facades, repositories, and abstract-class tokens. - Use Case: When building a feature that needs a configurable API base URL and a scoped state service, use this Skill to define an APP_CONFIG token, provide it in app.config.ts, and inject a component-scoped state service with proper cleanup via DestroyRef. ## Quick Start Ask the AI to create an Angular service using inject() with an injection token for the API URL and configure its providers 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 style in Angular v20+.

How do I create an InjectionToken in Angular?

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

What is the difference between providers and viewProviders in Angular?

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

How do I mock services in Angular tests with TestBed?

Pass a spy or stub via { provide: MyService, useValue: mockObject } in TestBed.configureTestingModule, or use .overrideProvider(TOKEN, { useValue: testValue }) to replace tokens. Then retrieve instances with TestBed.inject.

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. providedIn: 'root' creates a single application-wide singleton shared everywhere.