Reviews, designs, and refactors Angular dependency injection for services, providers, injectors, InjectionToken, inject(), testing overrides, standalone apps, NgModules, SSR, lazy loading, and provider scopes.
Ships in ngautopilot-angular. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use this skill to apply Angular Dependency Injection correctly so code stays decoupled, testable, maintainable, and compatible with the target Angular version.
This skill covers service creation, provider scope, injector hierarchy, InjectionToken, constructor injection, inject(), TestBed overrides, lazy-loaded boundaries, SSR-safe providers, and common DI failures such as NullInjectorError, duplicate providers, and circular dependencies.
Use this skill when:
providers in components, directives, modules, routes, bootstrap config, or tests.inject().NullInjectorError, circular dependencies, or unexpected duplicate service instances.new.Do not use this skill when:
angular.services.single-responsibility-services first.angular.state.signals-vs-rxjs or architecture skills first.Identify these inputs before recommending a DI pattern:
Angular version
architecture style: standalone, NgModules, or mixed
provider location: root, platform, bootstrap, route, NgModule, component, directive, or test
service lifetime: global singleton, feature scoped, route scoped, component scoped, or per test
runtime target: browser, SSR, library, microfrontend, or worker
testing stack: Jest, Jasmine/Karma, Vitest, or unknown
existing convention: constructor injection, inject(), or mixed
Inspect these files when relevant:
package.json
angular.json
app.config.ts
main.ts
app.module.ts
*.routes.ts
*.module.ts
*.component.ts
*.directive.ts
*.service.ts
*.spec.ts
Use version-aware DI guidance:
Angular 2-5:
use constructor injection and NgModule providers
avoid modern standalone and inject() patterns
Angular 6-13:
prefer @Injectable({ providedIn: 'root' }) for app-wide services
use constructor injection as the safest default
use NgModule providers for legacy app-wide or feature scope where required
Angular 14:
inject() is available in supported injection contexts
standalone APIs may exist but should not be assumed as the project default
constructor injection remains the most portable default
Angular 15-18:
standalone APIs are stable
providedIn, application providers, route providers, and inject() are valid when project style supports them
keep legacy compatibility when working inside NgModule-based code
Angular 19-21:
prefer current Angular DI style when the project is current
use providedIn, application providers, route providers, standalone providers, and inject() according to scope
still avoid experimental APIs unless the project opts into them
For shared libraries, choose the lowest supported Angular version of the consumers, not the newest version known by the agent.
Use this provider scope policy:
app-wide stateless service -> providedIn: 'root'
app-wide configuration value -> InjectionToken with root provider or application provider
feature orchestration -> route, feature, or facade provider based on lifecycle
component-local state -> component providers
view-only dependency override -> viewProviders when children should not see the provider
legacy module app -> NgModule providers when required by project style
standalone app bootstrap -> ApplicationConfig or bootstrapApplication providers
multi-extension point -> multi provider
test double -> TestBed provider override or fixture injector when component-scoped
Use constructor injection when:
the project targets Angular 2-13
the codebase convention favors constructors
dependencies are part of the explicit class contract
maximum readability and compatibility matter
Use inject() when:
the project supports Angular 14+
the code is in a valid injection context
the project convention allows it
the code is a functional guard, resolver, interceptor, factory, token factory, or field initializer
constructor length would add noise without improving clarity
Use InjectionToken when:
injecting interfaces, primitives, configuration, strategy functions, adapters, or abstract ports
the runtime token must exist after TypeScript erases types
multiple implementations may be swapped by provider configuration
Use this workflow:
1. Detect Angular version and project style.
2. Identify the dependency lifetime and injector scope.
3. Locate current provider declarations.
4. Detect manual instantiation, duplicate providers, and public mutable state.
5. Select constructor injection, inject(), or token-based injection.
6. Place the provider at the narrowest correct lifecycle scope.
7. Update tests with explicit provider overrides.
8. Explain risks around singleton behavior, SSR, lazy loading, and test isolation.
Use DI instead of manual construction:
@Component({
selector: "app-user-panel",
templateUrl: "./user-panel.component.html",
})
export class UserPanelComponent {
constructor(private readonly userService: UserService) {}
}
Use modern inject() only in supported contexts:
@Injectable({ providedIn: "root" })
export class UserFacade {
private readonly userApi = inject(UserApiService);
private readonly userState = inject(UserStateService);
}
Use root providers for true app-wide services:
@Injectable({
providedIn: "root",
})
export class UserApiService {}
Use component providers for isolated component state:
@Component({
selector: "app-user-panel",
templateUrl: "./user-panel.component.html",
providers: [UserPanelStateService],
})
export class UserPanelComponent {
private readonly state = inject(UserPanelStateService);
}
Use InjectionToken for configuration and ports:
export interface AppConfig {
apiUrl: string;
}
export const APP_CONFIG = new InjectionToken<AppConfig>("APP_CONFIG");
export const appConfig: ApplicationConfig = {
providers: [
{
provide: APP_CONFIG,
useValue: {
apiUrl: "https://api.example.com",
},
},
],
};
Use provider variants intentionally:
[
{ provide: LoggerPort, useClass: ConsoleLoggerService },
{ provide: API_URL, useValue: "https://api.example.com" },
{ provide: LoggerPort, useExisting: AppLoggerService },
{
provide: AuthStrategy,
useFactory: () => {
const config = inject(APP_CONFIG);
return config.useMockAuth
? new MockAuthStrategy()
: new RealAuthStrategy();
},
},
];
Use multi providers for extension points:
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
}
Mock dependencies explicitly in tests:
await TestBed.configureTestingModule({
declarations: [UserComponent],
providers: [
{
provide: UserService,
useValue: {
getUsers: jest.fn().mockReturnValue(of([])),
},
},
],
}).compileComponents();
When a provider is declared on the component, get the component-scoped instance:
const state = fixture.debugElement.injector.get(UserPanelStateService);
Avoid manual service construction:
export class UserComponent {
private readonly userService = new UserService();
}
Avoid placing every service in root:
@Injectable({ providedIn: "root" })
export class WizardStepStateService {}
Avoid public mutable implementation details:
export class UserStateService {
usersSubject = new BehaviorSubject<User[]>([]);
}
Avoid injecting TypeScript interfaces directly:
constructor(private readonly config: AppConfig) {}
Avoid using inject() outside an injection context:
ngOnInit(): void {
const userService = inject(UserService);
}
Avoid provider placement based only on "where it compiles". Provider location defines lifecycle.
Avoid circular service dependency chains:
AuthService -> UserService -> PermissionService -> AuthService
Use this format when reviewing DI:
## DI Diagnosis
- Angular version:
- Architecture style:
- Provider scope:
- Current issue:
## Recommended Pattern
- ...
## Code Proposal
- Provide the smallest compatible code change.
## Testing Impact
- ...
## Risks
- ...
## Validation Checklist
- ...
new.providedIn: 'root' when appropriate.InjectionToken.inject() is used only in valid injection contexts and compatible Angular versions.inject() can reduce compatibility for legacy projects.Problem:
NullInjectorError for APP_CONFIG.
Likely fix:
Create one exported InjectionToken and provide it once at application, route, module, or test scope.
Do not recreate the token in multiple files.
Problem:
Two components do not share expected state.
Likely cause:
The state service is listed in component providers, creating isolated instances.
Move it to root or a feature provider if shared lifecycle is intended.
Problem:
Unit test mock is ignored.
Likely cause:
The component declares its own provider.
Override the component provider or read the component-scoped service from fixture.debugElement.injector.
InjectionToken for runtime-safe configuration and abstract ports.What SKILL.md sets; muted values are Claude Code's defaults
/ngautopilot-angular:angular--dependency-injectionfrom ngautopilot-angular
npx claudepluginhub janpereira-dev/ngautopilot --plugin ngautopilot-angular350 skills in this plugin
/angular--ai--angular-v22-agent-skills-integrationAngular v22 Agent Skills IntegrationUse official Angular Agent Skills as reference material without copying them blindly or replacing NgAutoPilot routing. For Angular 22 projects and 21-to-22 upgrade planning.
/angular--ai--angular-v22-ai-tutor-safe-usageAngular v22 AI Tutor Safe UsageUse AI tutor workflows to teach Angular concepts without allowing unsafe code or secret exposure. For Angular 22 projects and 21-to-22 upgrade planning.
/angular--ai--angular-v22-angular-mcp-agent-workflowAngular v22 Angular MCP Agent WorkflowReview the workspace boundary, permissions, and runtime evidence of an Angular MCP integration. For Angular 22 projects and 21-to-22 upgrade planning.
Other plugins' skills for the same job