Dependency injection and change detection · mid
OnPush, Eager and ExpressionChangedAfterItHasBeenChecked
OnPush checks a component only when an input changes by reference, an event happens in it, or a signal it reads changes. In v22 OnPush is the default and the old behaviour is called Eager. NG0100 means data flowed backwards during a check.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- OnPush checks a child when an input changes by reference (
===), not when the contents of the same array change. pushkeeps the same array, so an OnPush child is skipped. Fix: an immutable update[...list, item], or a signal withupdate(list => [...list, item]).markForCheck()on the child also works but hides the real design problem.- In v22, components without a
changeDetectionsetting are OnPush.ng updatemarks existing onesEagerso old screens behave the same (facts.md). - Breaking patterns after v22: mutating an array that is passed as an input, and setting plain fields after async work without a signal.
- NG0100: in development Angular checks a second time. If a binding changed between the two checks, it throws. Something changed a parent value after the parent was checked.
- Typical cause: a child sets a shared
loadingflag inngAfterViewInit. It is not harmless: in production the screen may show a stale value.
The red pen
Common traps: mistake → fix
Mistake: OnPush never updates
Fix: it updates on input reference change, events and signals.
Mistake: NG0100 is only a dev warning I can ignore
Fix: production can show stale data.
Easy to hard
Questions interviewers ask, easy to hard
- What is the difference between Default (Eager) and OnPush change detection?
- Why does OnPush not see
array.push? - What is NG0100 and how do you fix it?
- After upgrading to v22, new screens do not refresh. Why?
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.