# 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. * Use `ChangeDetectionStrategy.OnPush` unless there is a concrete reason not to, in both zone.js and zoneless applications. * Keep component state minimal, update it immutably, and never mutate shared state directly. * 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. * Prefer explicit data flow: inputs down, events up. Avoid unnecessary two-way binding, component coupling, and exposed internal state. * Use lifecycle hooks only when required, and keep constructors free of complex initialization. ## 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 a rendering abstraction is actually needed. * 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, and avoid layout thrashing in performance-sensitive code.