Components and templates · core
Passing data between components and lifecycle hooks
Data goes down with inputs and up with outputs, and two-way with model(). Lifecycle hooks run in a fixed order, and API calls do not belong in the constructor.
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
- Parent to child:
input(),input.required()or the older@Input. Child to parent:output()or the older@OutputwithEventEmitter. - Two-way binding:
model()with[(value)]. - Siblings or far-away components share a service with signals (or a
BehaviorSubject), or a store. If two components are close, lift the state to their parent. - Hook order: constructor,
ngOnChanges,ngOnInit,ngDoCheck, content hooks, view hooks,ngOnDestroy. - The constructor is for injection and simple setup. Inputs are not set yet there.
ngOnInitruns once, after the first inputs are set. It is the usual place to start loading data (or use a resource).ngOnChangesruns on every input change for decorator inputs. Signal inputs usecomputedoreffectinstead.- Clean up in
ngOnDestroyor withDestroyRef.
The red pen
Common traps: mistake → fix
Mistake: I can read an input in the constructor
Fix: inputs are set after construction, so read them in
ngOnInitor in acomputed.Mistake: The child can change an input value
Fix: an input is read-only for the child; use
output()ormodel().
Easy to hard
Questions interviewers ask, easy to hard
- How does a child tell its parent that a button was clicked?
- What is the order of lifecycle hooks?
- Where should an API call start, and why not in the constructor?
- Two distant components must share a selected item. What are your options?
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.