What is the difference between NgZone and CDR in Angular

 NgZone vs ChangeDetectorRef (CDR) in Angular

Both NgZone and ChangeDetectorRef (CDR) help with change detection, but they serve different purposes.


🔹 NgZone

NgZone is a service that controls how Angular detects changes globally across the application. It helps manage execution inside or outside Angular's change detection system.

✅ When to Use NgZone?

  1. Running non-Angular events (like setTimeout, WebSockets, or third-party libraries) inside change detection.
  2. Skipping change detection for performance (e.g., animations, timers).

🔹 Key Methods:

  • run(callback) → Forces Angular to detect changes.
  • runOutsideAngular(callback) → Runs code outside change detection (for performance).

Example:

🟢 Using NgZone.run() to trigger change detection

constructor(private ngZone: NgZone) {}
someFunction() { this.ngZone.run(() => { this.someProperty = 'Updated Value'; // Change detection triggered }); }

🚀 Using runOutsideAngular() to improve performance

constructor(private ngZone: NgZone) {}
startTimer() { this.ngZone.runOutsideAngular(() => { setInterval(() => { console.log('Running outside Angular'); // No change detection triggered }, 1000); }); }

🔹 ChangeDetectorRef (CDR)

ChangeDetectorRef (CDR) is a service that allows manual control of change detection at the component level instead of affecting the entire application.

✅ When to Use ChangeDetectorRef?

  1. Manually triggering change detection when Angular doesn't detect changes.
  2. Detaching and reattaching change detection to improve performance.
  3. Fixing issues in OnPush change detection strategy.

🔹 Key Methods:

  • detectChanges() → Manually triggers change detection for the component.
  • markForCheck() → Marks the component for the next change detection cycle.
  • detach() → Stops change detection for the component.
  • reattach() → Re-enables change detection for the component.

Example:

🟢 Using detectChanges() to force change detection

constructor(private cdr: ChangeDetectorRef) {}
updateUI() { this.someValue = 'New Value'; this.cdr.detectChanges(); // Forces change detection }

🚀 Using detach() to improve performance

constructor(private cdr: ChangeDetectorRef) {}
ngOnInit() { this.cdr.detach(); // Stops change detection for this component } loadHeavyData() { // Do some processing... this.cdr.detectChanges(); // Manually trigger change detection when needed }

🔸 NgZone vs CDR: Key Differences

FeatureNgZone 🌀ChangeDetectorRef 🛠
ScopeGlobal (application-wide)Component-level
PurposeControls execution inside/outside Angular's change detectionManages when and how a component detects changes
Performance ImpactCan prevent unnecessary change detection for performanceCan pause/resume change detection for a specific component
Best Use CasesHandling async tasks (WebSockets, setTimeout, external libraries)Manually controlling or optimizing change detection for components

🔹 When to Use Which?

Use NgZone when dealing with external async operations (e.g., WebSockets, timers, third-party libraries).
Use ChangeDetectorRef when optimizing component rendering (e.g., detaching detection for better performance).

No comments:

Post a Comment