Understand changeDetection in Angular

Anh-Thi Dinh
draft
⚠️
This is a quick & dirty draft, for me only!
This note is for understanding changeDetection strategy in Angular with examples.
  • How to use?
    • 1@Component({
      2  // ... others
      3  changeDetection: ChangeDetectionStrategy.OnPush 
      4  // default to .Default (CheckAlways)
      5  // .OnPush (CheckOnce)
      6})
      7export class NameOfComponent implements OnInit {}
  • .Default ensures the accuracy (view updated when having changes), it can lead to performance → use .OnPush to optimize!
  • When for some reason, the view isn’t reflected with the changes, use detectChanges()!
  • If you want to detach change detector to limit how often check occurs (and reattach later), read the official guide.