Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My breakpoint in Angular's change detection is not triggered on checkAndUpdateView()

Following this good article on angular's change detection, I wanted to debug the checkAndUpdateView function. However, it is never triggered.

enter image description here

What am I missing? I tried with the latest Chrome and Firefox.

like image 668
Tim Avatar asked Oct 01 '20 14:10

Tim


People also ask

What are the ways to trigger change detection in Angular?

By default, angular will run the change detector every time @Input() data is changed or modified. But with OnPush strategy, the change detector is only triggered if the data passed on @Input() has a new reference.

How does Angular detect change detection?

To run the change detector manually: Inject ChangeDetectorRef service in the component. Use markForCheck in the subscription method to instruct Angular to check the component the next time change detectors run. On the ngOnDestroy() life cycle hook, unsubscribe from the observable.

How do you use change detection OnPush?

An OnPush change detector gets triggered in a couple of other situations other than changes in component Input() references, it also gets triggered for example: if a component event handler gets triggered. if an observable linked to the template via the async pipe emits a new value.

What is the default component change detection strategy in Angular?

By default, Angular 2+ performs change detection on all components (from top to bottom) every time something changes in your app. A change can occur from a user event or data received from a network request.


1 Answers

That article is old, from 2018, and since then Angular has introduced the Ivy compiler, which completely overhauled the internals of Angular. If you are using Angular 9 or later this breakpoint won't be hit. I tested an Angular 7, 8 and 9 app. Versions 7 & 8 hit the breakpoint, and the Angular 9 app didn't.

I would suggest using this component to debug change detection. Add it to your app and it will count change detection cycles.

debug-change-detection.component.ts:

import { Component, NgZone } from '@angular/core';

@Component({
  selector: 'app-debug-change-detection',
  template: '<p class="number">{{check()}} zone checks</p>',
  styles: [`
        :host {
          position: absolute;
          left: 10px;
          bottom: 0;
          display: block;
        }
        .number {
          display: block;
        }
    `]
})

export class DebugChangeDetectionComponent {

  count = 0;

  constructor(private zone: NgZone) { }

  check() {
    this.zone.runOutsideAngular(() => {
      setTimeout(() => this.count = this.count + 1);
    });
    return this.count;
  }
}
like image 82
inorganik Avatar answered Oct 17 '22 12:10

inorganik