Cursor rules for Angular v22 components using standalone imports, signal inputs and outputs, inject(), OnPush, tracked @for loops and typed reactive forms.
.cursor/rules/angular-component-standards.mdc. Downloaded files need to be placed in that repository path.---
description: "Cursor rules for Angular v22 components using standalone imports, signal inputs and outputs, inject(), OnPush, tracked @for loops and typed reactive forms."
globs: "**/*"
alwaysApply: false
---
# Angular Component Standards
Applies to: Angular components, their templates and the services they use (`**/*.ts`, `**/*.html` in an Angular workspace).
Version baseline: checked against the Angular v22 documentation and style guide (v21 is LTS). Read `package.json` (`@angular/core`) and `angular.json` first. Before v19, components are not standalone by default; before v17 there is no `@if`/`@for`. Follow the installed version and the file-naming pattern the workspace already uses.
## Responsibilities
- A component owns presentation and user interaction for one piece of UI. Put HTTP calls, caching and business rules in injectable services.
- Split a component when its template has independent sections with their own state, or when part of it is reused. Child components replace deeply nested template logic.
- Keep components decoupled from concrete data sources: inputs in, outputs out, services injected through DI.
## Component shape (standalone, signals)
```ts
import { ChangeDetectionStrategy, Component, computed, inject, input, output } from '@angular/core';
import { UserStore } from './user-store';
export interface User {
id: string;
name: string;
active: boolean;
}
@Component({
selector: 'app-user-list',
imports: [],
templateUrl: './user-list.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserList {
private readonly store = inject(UserStore);
readonly users = input.required<User[]>();
readonly showInactive = input(false);
readonly selected = output<User>();
protected readonly visibleUsers = computed(() =>
this.showInactive() ? this.users() : this.users().filter((u) => u.active),
);
protected select(user: User): void {
this.store.markSeen(user.id);
this.selected.emit(user);
}
}
```
```html
<!-- user-list.html -->
@if (visibleUsers().length > 0) {
<ul>
@for (user of visibleUsers(); track user.id) {
<li>
<button type="button" (click)="select(user)">{{ user.name }}</button>
</li>
}
</ul>
} @else {
<p>No users to show.</p>
}
```
- Components are standalone by default (v19+); list dependencies in `imports`. Do not add `standalone: true` or NgModules to new code.
- Use `input()`, `input.required()`, `output()` and `model()` instead of `@Input()`/`@Output()` decorators in new code; mark them `readonly`.
- Use `inject()` for dependencies. `@Injectable({ providedIn: 'root' })` marks a service as injectable; it does not inject anything into a component.
- Mark members used only by the template `protected`.
- Use `ChangeDetectionStrategy.OnPush` with signals and immutable inputs.
- Use built-in control flow (`@if`, `@for`, `@switch`). Every `@for` needs a `track` expression with a stable unique key such as an ID; `track $index` is only for static lists.
## Naming and files
- Class names in PascalCase that describe the UI (`UserList`). File names in kebab-case.
- The current style guide names a component file after the class without a `.component` suffix (`user-list.ts`, `user-list.html`, `user-list.css`). Workspaces generated before v20 often use `user-list.component.ts`; keep whichever convention the workspace already uses and do not rename existing files as a side effect.
- Keep a component's TypeScript, template, styles and spec together in one feature directory.
## Types
- No `any`. Type inputs, outputs and service return values; use interfaces for API response shapes.
- Turn on `strictTemplates` in `angularCompilerOptions` so template expressions are type-checked.
## Lifecycle and async work
- Constructors and field initializers are for `inject()` and signal setup. Use `ngOnInit` when work depends on input values, or derive it with `computed`.
- Read HTTP data through services. In templates, prefer signals (`toSignal`, `resource`/`httpResource` where available in your version) or the `async` pipe over manual `subscribe`.
- When you must subscribe in a component, end the subscription with `takeUntilDestroyed()`.
- Handle request errors in the service or with an error state signal, and show a user-facing message; do not leave rejected observables unhandled.
## Forms
- Use reactive forms (`FormGroup`, `FormControl`, typed with `NonNullableFormBuilder`) for forms with validation, dynamic fields or tests.
- Template-driven forms with `[(ngModel)]` are acceptable for very small forms; do not mix both styles in one form.
- Use `[(value)]` two-way binding with `model()` inputs for component-to-component two-way state; use one-way bindings (`{{ }}`, `[prop]`) for display.
## Verify before finishing
Run these when they are available in the workspace:
- `ng build` (with `strictTemplates`, template type errors fail here)
- `ng test` for the touched component
- `ng lint` (angular-eslint), which also reports unused imports
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
An Angular v22 workspace has this rule attached to component files while a developer asks Cursor to build a filtered list component.
Create a UserList component that takes users and a showInactive flag, hides inactive users unless the flag is set, emits the clicked user and tells UserStore the user was seen.
Illustrative; not a recorded model run. A standalone OnPush component with readonly input.required<User[]>(), input(false) and output<User>(); UserStore obtained with inject(); a computed visibleUsers signal; a template using @if/@else and @for with track user.id and a type="button" element; protected template members; file names following the workspace's existing convention; no NgModule, no @Input/@Output decorators, no any.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/angular-component-standards.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
---
description: "Angular Component Standards: Standalone, Signals and Control Flow project conventions"
globs: "**/*"
alwaysApply: false
---
# Angular Component Standards
Applies to: Angular components, their templates and the services they use (`**/*.ts`, `**/*.html` in an Angular workspace).
Version baseline: checked against the Angular v22 documentation and style guide (v21 is LTS). Read `package.json` (`@angular/core`) and `angular.json` first. Before v19, components are not standalone by default; before v17 there is no `@if`/`@for`. Follow the installed version and the file-naming pattern the workspace already uses.
## Responsibilities
- A component owns presentation and user interaction for one piece of UI. Put HTTP calls, caching and business rules in injectable services.
- Split a component when its template has independent sections with their own state, or when part of it is reused. Child components replace deeply nested template logic.
- Keep components decoupled from concrete data sources: inputs in, outputs out, services injected through DI.
## Component shape (standalone, signals)
```ts
import { ChangeDetectionStrategy, Component, computed, inject, input, output } from '@angular/core';
import { UserStore } from './user-store';
export interface User {
id: string;
name: string;
active: boolean;
}
@Component({
selector: 'app-user-list',
imports: [],
templateUrl: './user-list.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserList {
private readonly store = inject(UserStore);
readonly users = input.required<User[]>();
readonly showInactive = input(false);
readonly selected = output<User>();
protected readonly visibleUsers = computed(() =>
this.showInactive() ? this.users() : this.users().filter((u) => u.active),
);
protected select(user: User): void {
this.store.markSeen(user.id);
this.selected.emit(user);
}
}
```
```html
<!-- user-list.html -->
@if (visibleUsers().length > 0) {
<ul>
@for (user of visibleUsers(); track user.id) {
<li>
<button type="button" (click)="select(user)">{{ user.name }}</button>
</li>
}
</ul>
} @else {
<p>No users to show.</p>
}
```
- Components are standalone by default (v19+); list dependencies in `imports`. Do not add `standalone: true` or NgModules to new code.
- Use `input()`, `input.required()`, `output()` and `model()` instead of `@Input()`/`@Output()` decorators in new code; mark them `readonly`.
- Use `inject()` for dependencies. `@Injectable({ providedIn: 'root' })` marks a service as injectable; it does not inject anything into a component.
- Mark members used only by the template `protected`.
- Use `ChangeDetectionStrategy.OnPush` with signals and immutable inputs.
- Use built-in control flow (`@if`, `@for`, `@switch`). Every `@for` needs a `track` expression with a stable unique key such as an ID; `track $index` is only for static lists.
## Naming and files
- Class names in PascalCase that describe the UI (`UserList`). File names in kebab-case.
- The current style guide names a component file after the class without a `.component` suffix (`user-list.ts`, `user-list.html`, `user-list.css`). Workspaces generated before v20 often use `user-list.component.ts`; keep whichever convention the workspace already uses and do not rename existing files as a side effect.
- Keep a component's TypeScript, template, styles and spec together in one feature directory.
## Types
- No `any`. Type inputs, outputs and service return values; use interfaces for API response shapes.
- Turn on `strictTemplates` in `angularCompilerOptions` so template expressions are type-checked.
## Lifecycle and async work
- Constructors and field initializers are for `inject()` and signal setup. Use `ngOnInit` when work depends on input values, or derive it with `computed`.
- Read HTTP data through services. In templates, prefer signals (`toSignal`, `resource`/`httpResource` where available in your version) or the `async` pipe over manual `subscribe`.
- When you must subscribe in a component, end the subscription with `takeUntilDestroyed()`.
- Handle request errors in the service or with an error state signal, and show a user-facing message; do not leave rejected observables unhandled.
## Forms
- Use reactive forms (`FormGroup`, `FormControl`, typed with `NonNullableFormBuilder`) for forms with validation, dynamic fields or tests.
- Template-driven forms with `[(ngModel)]` are acceptable for very small forms; do not mix both styles in one form.
- Use `[(value)]` two-way binding with `model()` inputs for component-to-component two-way state; use one-way bindings (`{{ }}`, `[prop]`) for display.
## Verify before finishing
Run these when they are available in the workspace:
- `ng build` (with `strictTemplates`, template type errors fail here)
- `ng test` for the touched component
- `ng lint` (angular-eslint), which also reports unused imports
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.