4

I have a simple angular2 component that contains only an ag-grid component with some data bind to it.

My component implement the DoCheck and the ngDoCheck method is implement in the component.

export class AppComponent implements DoCheck 
{
    ngDoCheck() {
        console.log('do check..');
    }
} 

I have realized that the ngDoCheck function is always running endlessly.

Any explanation about this issue.

Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60

2 Answers2

3

This hook is called with enormous frequency — after every change detection cycle no matter where the change occurred.

Is a really dirty cycle, in fact even angular itself is suggesting to use more proper way to implement custom checks.

https://angular.io/docs/ts/latest/api/core/index/DoCheck-class.html

Valex
  • 1,149
  • 1
  • 8
  • 14
  • I'm having the same issue. Wouldn't it still be firing constantly even if we didn't use this hook. Is there a way of finding out what is changing endlessly? – abyrne85 Nov 05 '18 at 09:51
  • well, do check runs every time the change detection run, this answer gives some hint to how check what is triggering the change detection https://stackoverflow.com/questions/43661242/is-there-any-way-to-find-out-what-is-triggering-change-detection-in-angular-2 , alternatively in some cases might be useful control the change detection manually by change the change detection strategy in that component ( and its children ) by setting @component({changeDetection:ChangeDetectionStrategy.OnPush}) and then control it manually with ChangeDetectorRef – Valex Nov 08 '18 at 08:05
  • Possible cause makes it run infinitely is that you might have used ChangeDetectorRef in any of your components and you might not have unsubscribed subscription variables. – Dan Patil Mar 12 '19 at 09:11
1

In my case ngAfterViewChecked was called endlessly because of sentry.js which do monkey patching of console.log which triggers Change Detection. I pulled off half of my hair till noticed that code below causes loop because of sentry.

ngAfterViewChecked () {

    console.log('Rendered');

}
Dzmitry Vasilevsky
  • 1,295
  • 2
  • 14
  • 25