Files
MyAiConfiguration/shared/rules/angular/references/components-and-state.md
T
Julian LechnerandClaude Opus 5.5 6159c6373c
CI / CI configuration security checks (pull_request) Successful in 2s
CI / Capabilities and shell quality (pull_request) Successful in 15s
CI / Python tests and compilation (pull_request) Successful in 36s
CI / QMD unit tests (pull_request) Successful in 7s
CI / Build and inspect generated packages (pull_request) Successful in 16s
CI / Bash integration and installation guards (pull_request) Failing after 51s
CI / PowerShell tests on Ubuntu (pull_request) Failing after 1m45s
CI / Installed client configuration smoke tests (pull_request) Canceled after 1m24s
docs(rules): fix contradictions, update technical rules, enforce rule structure
- Resolve contradictions: authorization is never a skipped "boundary check";
  one browser token policy (BFF/HttpOnly cookies first, RFC 10017); one
  sanitizer-bypass rule; Angular responses are unknown until validated.
- Add security rules for AI/LLM integration and Java XXE, concrete crypto
  and password-hashing parameters, and RFC 8725 JWT validation.
- Update version-dependent guidance after checking official sources:
  Angular 22, TypeScript 6/7, Node 24+, .NET 10/C# 14, JDK 21-25, Spring
  Boot 4; add a Spring Boot and Spring Security topic.
- Give every rule a unique level-1 title and scope line, keep language
  security files to language specifics, and fix routing for inbound vs
  outbound HTTP, XML parsing, and WPF/Angular parent rules.
- Validate rule structure and routing in validate-config.py, with tests.
- Fix CI issues found by a local run: shellcheck directive order, pinned
  ruff rule set, Invoke-Python test mock, CRLF-tolerant package checks,
  Windows-only credential test, and refreshed prompt-budget baselines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 10:51:08 +01:00

4.0 KiB

Angular Components, Change Detection, and Signals

Apply these rules when creating or changing Angular components, directives, signal state, change detection, or DOM access.

Components

  • Prefer standalone components, directives, and pipes for new work. Avoid introducing NgModule unless the existing architecture requires it.
  • Keep components small; move substantial business logic to a focused service or domain layer.
  • Keep dependencies explicit and follow the feature architecture. Avoid god components, god services, unnecessary wrappers, and abstractions without a concrete need.
  • Use ChangeDetectionStrategy.OnPush unless there is a concrete reason not to, in both zone.js and zoneless applications.
  • Keep component state minimal and prefer immutable state updates.
  • Avoid direct mutation of shared state.
  • Keep inputs and outputs strongly typed.
  • In new code on Angular 19+, use input() and input.required(), output() (an OutputEmitterRef, not a signal), model() for intended two-way bindings, and signal queries (viewChild, viewChildren, contentChild, contentChildren). Keep @Input, @Output, and decorator queries only where the surrounding code uses them.
  • Use the host object in component metadata instead of @HostBinding and @HostListener in new code.
  • Avoid unnecessary two-way binding.
  • Avoid unnecessary component-to-component coupling.
  • Prefer explicit data flow from parent to child and explicit events upward.
  • Do not expose internal component state unnecessarily.
  • Use lifecycle hooks only when required.
  • Avoid complex initialization logic inside constructors.

Change Detection

  • Check whether the application uses zone.js or is zoneless: new applications are zoneless by default since Angular 21 (provideZonelessChangeDetection, stable since 20.2). Do not migrate between the two as a side effect of another task.
  • In zoneless applications, change template-bound state only through signals, markForCheck, the async pipe, or template event handlers. Mutating plain fields in setTimeout or promise callbacks does not update the view.
  • Do not add NgZone APIs (onStable, onMicrotaskEmpty, runOutsideAngular) for rendering logic; use afterNextRender or afterRenderEffect for DOM work after rendering.

Signals and State

  • Prefer signals for local and synchronous reactive state.
  • Derive values with computed instead of storing or duplicating them; keep computed free of side effects.
  • Keep signals private when external mutation is not intended, and expose readonly state where practical.
  • Avoid circular signal dependencies.
  • Use effect only for actual side effects, not as a replacement for normal data flow. Keep effects small and deterministic, and clean up resources they create.
  • Use linkedSignal (Angular 20+) for writable state that resets when a source changes, instead of an effect that writes signals.
  • For parameter-driven reads, use httpResource, resource, or rxResource (stable since Angular 22; experimental in 19 to 21, so use them there only if the project already does). Use RxJS for event streams, cancellation semantics, and multi-step flows; convert with toSignal at the component edge, in an injection context. Never perform writes or mutations through resources.
  • Do not use global state unless multiple unrelated parts of the application genuinely need it.
  • Do not introduce a state-management library without a clear requirement.

DOM and Browser APIs

  • Prefer Angular APIs and declarative templates over direct DOM manipulation.
  • Use Renderer2 only when direct rendering abstraction is actually needed.
  • Avoid accessing globals directly when it harms testability or SSR compatibility.
  • When the application uses SSR, guard window, document, localStorage, and similar browser-only APIs with isPlatformBrowser, or run DOM access in afterNextRender or afterRenderEffect, which run only in the browser.
  • Clean up event listeners, observers and timers.
  • Avoid layout thrashing in performance-sensitive code.