The question
Is Angular designed to constantly re-check everything in order to detect changes? I'm coming from the React world and I was expect something like event triggered -> re-rendering
. I don't know if this is something from my app or something from Angular.
If I have a method that's called from the HTML Template, it gets called infinitely even if nothing in my component changes.
The problem
I have a calendar-like page that loads a lot of data and has to compute things before rendering, because it would be too difficult to do these things with the ngIf
directive. So I have things in my template that look like:
<div [innerHTML]="_getDayPrice(item, day) | safeHtml"></div>
If I console log something in the _getDayPrice
method, it gets printed infinitely.
What I've tried
I've managed to bypass this by manually injecting the
ChangeDetectionRef
in my app and doingthis.cdRef.detach()
. This however feels hacky as sometimes I might need to re-enable it and detach again.I've tried to investigate if it's something from my parent component apps, like containers. I've rendered in my main app.component a single div like
<div class={{computeClass()}}>
and in that method printed a console log and sure enough it gets called infinitely. So following this I've tried commenting-out all of the apps' services. If all are commented out sure enough it works properly, but also there's no observable data. I've investigated for about half a day and couldn't find a single point of failure (like commenting out this service fixes everything).Record performance using chrome's built-in Performance tab, but again couldn't find anything from my code that triggers changes.
zone.js
gets called repeatedly and appears to set an interval that keeps firing.Of course I've searched for occurrences of
setTimeout
andsetInterval
in the services but couldn't find something that keeps getting changed that might cause this issue.
Conclusion?
Bottom line is: is it normal, if you have a complex Angular app and call a method from the template, for that method to be called infinitely?
And if not, do you have any hints as to what might be causing this? Or any other means to bypass it rather than detaching the changeRef detector?
My only concern is with performance. It's a calendar-like page that renders multiple-rows, and it lags pretty severely on a 8GB RAM laptop. I'm pretty sure that a tablet or a phone would almost freeze.
_getDayPrice
doesn't call a web service you want to create a proper component instead of what you are doing now. – Mccool