Angular core · Scenario
ExpressionChangedAfterItHasBeenCheckedError after adding a loader
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
A teammate added a loading flag to the shared layout. Now the console shows 'ExpressionChangedAfterItHasBeenCheckedError' on every page in development. Production looks fine. What's going on, and what do you do?
Then
Follow-ups the interviewer asks next
- Why does it show only in development?
- Read the error with me. What do 'previous value' and 'current value' tell you?
- Where is the value being changed, and why is that too late?
- A teammate wraps the change in
setTimeoutand the error goes away. Is that a fix? - What is the proper fix here?
- How would signals change this?
The answer
What a strong answer covers
- In development, Angular runs an extra check after each change-detection pass. If a binding's value changed between the two, it throws NG0100. It means data flowed "backwards": something changed a parent's binding after the parent was already checked.
- It is not harmless: in production the screen can show a stale value until the next check.
- Cause here: a page component sets
loading = trueon a shared service inngAfterViewInit, after the layout above it was already rendered. - Fixes, in order: make the change earlier (
ngOnInitor when the request starts), or derive the value instead of setting it, or hold the shared state in a signal so a change schedules a new check instead of changing a value in the middle of one. setTimeoutorPromise.resolve().then()only hides it by moving the change to a later round, and it can flicker.detectChanges()in the child is also a patch.
The red pen
Common traps
Avoid:"it's only in dev, ignore it"; setTimeout as the fix; turning off dev mode.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.