Skip to the page

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.

The notes

What you must know

  • Parent to child: input(), input.required() or the older @Input. Child to parent: output() or the older @Output with EventEmitter.
  • 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.
  • ngOnInit runs once, after the first inputs are set. It is the usual place to start loading data (or use a resource).
  • ngOnChanges runs on every input change for decorator inputs. Signal inputs use computed or effect instead.
  • Clean up in ngOnDestroy or with DestroyRef.

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 ngOnInit or in a computed.

  • Mistake: The child can change an input value

    Fix: an input is read-only for the child; use output() or model().

Easy to hard

Questions interviewers ask, easy to hard

  1. How does a child tell its parent that a button was clicked?
  2. What is the order of lifecycle hooks?
  3. Where should an API call start, and why not in the constructor?
  4. 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 voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview