Skip to the page

Components and templates · mid

Content projection, style scoping and directives

Content projection lets a parent put markup inside a child. View encapsulation scopes styles to one component. A directive adds behaviour to an existing element.

The notes

What you must know

  • <ng-content /> is a slot. Extra slots use a CSS selector: <ng-content select="[card-header]" />. ngProjectAs matches a slot when the content is wrapped.
  • <ng-content> can hold fallback content, since v18 (bank answer).
  • Projected content is created by the parent, always, even when the child does not show it.
  • Emulated encapsulation (the default) adds attributes such as _ngcontent-... to the component own elements and rewrites selectors to match only them.
  • A library component inner elements belong to the library template, so your selector does not match them. ::ng-deep turns scoping off for the rest of the selector, but it is deprecated in practice; prefer the library theming tokens or CSS variables.
  • A directive with host: { "(document:click)": "..." } can implement click-outside. It uses output() and inject(ElementRef). Host listeners are removed when the directive is destroyed.
  • Check !el.nativeElement.contains(event.target) before emitting.

The red pen

Common traps: mistake → fix

  • Mistake: Projected content is created only when the child shows it

    Fix: the parent always creates it.

  • Mistake: ::ng-deep is the normal fix for library styles

    Fix: it is a last resort and leaks styles; use CSS variables or the library theming first.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is content projection and when do you use it?
  2. Why do my styles not reach the inside of a library component?
  3. How do you write a click-outside directive?
  4. How many projection slots can a component have?

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