5

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.

Tim
  • 3,910
  • 8
  • 45
  • 80
  • can you provide more context and share code? which component, how is configured, its code, how you attempt to trigger detection etc. – Sergey Rudenko Oct 01 '20 at 14:49

1 Answers1

4

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;
  }
}
inorganik
  • 24,255
  • 17
  • 90
  • 114
  • Oh I see. I already have all this, I just wanted to debug it manually to understand what is happening under the hood. So there is no way to debug it in the Browser since Ivy? – Tim Oct 01 '20 at 15:48
  • I don't think there is one breakpoint that will help you understand what's happening under the hood. It's pretty complex.... – inorganik Oct 01 '20 at 17:28
  • Well, there was until ivy came along. checkAndUpdateView is what I am looking for. How come a breakpoint won't stop it anymore? I doubt they changed the whole change detection mechanism. – Tim Oct 01 '20 at 17:35
  • 1
    i just googled "ivy change detection breakpoint" and found this: same author: https://indepth.dev/ivy-engine-in-angular-first-in-depth-look-at-compilation-runtime-and-change-detection/ – inorganik Oct 01 '20 at 20:21
  • 1
    I found that too :) I thought you knew more about it. I'll accept your answer, "Versions 7 & 8 hit the breakpoint, and the Angular 9 app didn't." helped me. Thanks – Tim Oct 01 '20 at 20:26