'Infinite loop when calling detectChanges() inside ngAfterViewChecked?
I recently reviewed some code where detectChanges()
is called from ngAfterViewChecked
. How does that not cause an infinite loop? Shouldn't it trigger another change detection cycle, which will call ngAfterViewChecked
again? Or does the change detection only apply to the view, and therefore ngAfterViewChecked
will only be called on the child components, not the host component?
Solution 1:[1]
You already answered it yourself: If you call detectChanges()
in a component, the change detection applies only to this view. You could theoretically cause an infinite loop by calling ApplicationRef.tick()
instead, which triggers change detection on the root, but Angular catches this and throws the error below:
A good deep-in-the-code overview of change detection is given in this article.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | slim |