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.
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
<ng-content />is a slot. Extra slots use a CSS selector:<ng-content select="[card-header]" />.ngProjectAsmatches 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-deepturns 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 usesoutput()andinject(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-deepis the normal fix for library stylesFix: 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
- What is content projection and when do you use it?
- Why do my styles not reach the inside of a library component?
- How do you write a click-outside directive?
- 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 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.