Provides Angular best practices for components, modules, services, and reactive patterns. Use when working with Angular TypeScript files, component templates, NgModules, RxJS observables, or when the user mentions Angular, ng, or Angular CLI.
name: angular-best-practices
type: reference
description: "Provides Angular best practices for components, modules, services, and reactive patterns. Use when working with Angular TypeScript files, component templates, NgModules, RxJS observables, or when the user mentions Angular, ng, or Angular CLI."
paths: ["/*.component.ts", "/.service.ts", "**/.module.ts", "**/angular.json"]
effort: 3
allowed-tools: Read, Glob, Grep, Write, Edit, Bash
user-invocable: true
when_to_use: "When building Angular applications or working with RxJS streams"
Angular Best Practices
Critical rules (non-obvious)
Always unsubscribe from Observables in ngOnDestroy — use takeUntilDestroyed() (Angular 16+) or Subject + takeUntil
ChangeDetectionStrategy.OnPush: component only updates when input reference changes or async pipe emits — use for all leaf components
Never mutate input objects/arrays: OnPush won't detect mutation; create new reference instead
trackBy is mandatory on *ngFor with dynamic lists — without it, every change re-renders all DOM nodes
async pipe auto-unsubscribes — prefer it over manual subscription in templates
// switchMap: cancels previous — good for search, bad for saves
search$.pipe(
debounceTime(300),
distinctUntilChanged(),
switchMap(term => this.api.search(term)) // cancels in-flight request on new input
)
// exhaustMap: ignores new while processing — good for login button
loginClick$.pipe(
exhaustMap(() => this.auth.login(credentials)) // prevents double-submit
)
// mergeMap: parallel — good for independent operations
ids$.pipe(mergeMap(id => this.api.fetch(id), 3)) // 3 concurrent max
// combineLatest vs withLatestFrom:
// combineLatest: emits when ANY source emits
// withLatestFrom: emits only when primary source emits, takes latest from secondary
primary$.pipe(withLatestFrom(secondary$)) // common for "take latest filter value on button click"