- Home
- Skills
- Analogjs
- Angular Skills
- Angular Di
angular-di_skill
101
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill analogjs/angular-skills --skill angular-di- SKILL.md8.3 KB
Overview
This skill implements dependency injection patterns for Angular v20+ using inject(), InjectionToken, and provider configuration. It helps you design service architecture, control provider scope (root, component, route), and create configurable tokens and factories. Use it to manage singletons, scoped instances, multi providers, and advanced injector scenarios.
How this skill works
The skill inspects service and component creation flows, provider declarations, and token usage to recommend correct provider types (useClass, useValue, useFactory, useExisting). It highlights where to apply providedIn versus explicit providers, how to create and inject tokens, and when to use optional/self/skipSelf/host injection options. It also covers advanced APIs: multi providers, APP_INITIALIZER, environment injectors, and runInInjectionContext for runtime DI.
When to use it
- Designing app-wide singletons or scoped services (root vs component vs route).
- Creating configuration tokens or environment-specific values with InjectionToken.
- Replacing implementations via useClass/useExisting for testing or platform variation.
- Collecting plugins or validators with multi providers.
- Running initialization tasks before app start with APP_INITIALIZER.
Best practices
- Prefer inject() over constructor injection for concise, testable classes and factory functions.
- Use providedIn: 'root' for true singletons; use component or route providers for per-instance state.
- Prefer InjectionToken for typed, named configuration and self-providing tokens with factories for globals (window, localStorage).
- Use useFactory with explicit deps for complex instantiation; avoid async factories—use APP_INITIALIZER instead.
- Prefer multi providers for extensibility (interceptors, validators) and use useExisting to alias implementations without extra instances.
Example use cases
- Provide an API base URL via an InjectionToken and swap values per environment using useValue.
- Register a per-component EditorStateService to keep multiple editor instances isolated.
- Expose multiple validation rules using a multi VALIDATORS token and aggregate them in a ValidationService.
- Create a platform WINDOW token with a factory so services can access window safely and be testable.
- Use APP_INITIALIZER to load remote configuration before boot and register services based on that config.
FAQ
Use providedIn for most singletons; it enables tree-shaking and simpler code. Add providers in ApplicationConfig when you need conditional registration, environment-specific overrides, or to provide values (useValue/useFactory) at app bootstrap.
Can I use async factories for providers?
Async factories are possible but not recommended; they delay injector resolution. Prefer APP_INITIALIZER to run async startup work and then register synchronous providers if needed.