angular
✓Verified·Scanned 2/18/2026
Avoid common Angular mistakes — subscription leaks, change detection, dependency injection, and module organization.
from clawhub.ai·vb91a02e·3.2 KB·0 installs
Scanned from 1.0.0 at b91a02e · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/angular
Subscription Leaks
- Manual subscribe needs unsubscribe — in
ngOnDestroyor usetakeUntilDestroyed() asyncpipe auto-unsubscribes — prefer over manual subscribe in templatestakeUntilDestroyed()in inject context — cleaner than Subject + takeUntil pattern- HTTP observables complete automatically — but others don't, always handle cleanup
Change Detection
- Default checks entire component tree — expensive with large apps
OnPushonly checks on input change or event — addchangeDetection: ChangeDetectionStrategy.OnPush- Mutating objects doesn't trigger OnPush — create new reference:
{...obj}or[...arr] markForCheck()to manually trigger — when async changes data outside Angular
Dependency Injection
providedIn: 'root'for singleton services — tree-shakeable, no module registration needed- Component-level
providerscreates new instance — per component, not shared @Optional()for optional dependencies — prevents error if not provided@Inject(TOKEN)for injection tokens — not just classes
Lifecycle Hooks
ngOnInitafter inputs set — use for initialization, not constructorngOnChangesbeforengOnInit— called on every input change, receivesSimpleChangesngAfterViewInitfor DOM access —@ViewChildnot available until thenngOnDestroyfor cleanup — subscriptions, timers, event listeners
Templates
*ngForneedstrackByfor performance — prevents re-rendering entire listtrackByfunction returns unique identifier —trackByFn = (i, item) => item.id@ifand@for(Angular 17+) replace*ngIf/*ngFor— better performance, cleaner syntaxng-containerfor structural directives — no DOM element added
Reactive Forms
FormGroupandFormControlfor reactive — not two-way binding- Validators at control level —
Validators.required,Validators.email valueChangesis Observable — subscribe or use async pipepatchValuefor partial update —setValuerequires all fields
Modules vs Standalone
- Standalone components don't need module —
standalone: truein decorator - Import directly in other standalone components — no module declaration needed
- Mixing: standalone can import modules — modules can import standalone
- New projects prefer standalone — modules for legacy or complex DI
Routing
- Lazy load with
loadComponentorloadChildren— reduces initial bundle - Guards return
boolean,UrlTree, or Observable —UrlTreefor redirects - Resolvers pre-fetch data — available in
ActivatedRoute.data - Route params:
snapshotonce,paramMapObservable — for navigation without destroy
Common Mistakes
ElementRef.nativeElementdirect DOM access — breaks SSR, use Renderer2setTimeoutoutside Angular zone — useNgZone.run()or change detection won't trigger- Circular dependency in DI — use
forwardRef()or restructure HttpClientmethods return cold Observable — each subscribe makes new request