Sub topic 12
Pitfalls and design guidelines
This chapter assumes you can write standalone Angular components. It also assumes you know ng-content, the select attribute, fallback content and content queries from the earlier chapters. Here you will learn the mistakes that come up most often when building wrapper components, and a few rules for avoiding them.
Pitfall 1: select only matches direct children
A select selector is tested against the top-level nodes the parent places between the component tags. It does not search deeper. Wrapping an element in a div hides it from the selector.
<app-panel>
<div class="header-group">
<h2 panel-title>Settings</h2>
</div>
<button panel-actions>Save</button>
</app-panel>The div has no panel-title attribute, so it goes to the default slot, and the h2 goes with it. Only the button lands in its named slot. Two fixes work:
- Remove the wrapper.
- Replace it with
<ng-container ngProjectAs="[panel-title]">, which adds no element to the DOM.
Pitfall 2: unmatched content disappears
If a node matches no select and the component has no plain <ng-content />, Angular drops it. It reports no error, so the content is silently missing from the page. If your component should accept anything, always include a default slot. If it should be strict, say so in its documentation.
Pitfall 3: expecting conditional slots to be lazy
Wrapping ng-content in @if hides the content but does not stop the parent from creating it:
import { Component, input } from '@angular/core';
@Component({
selector: 'app-disclosure',
template: `
@if (open()) {
<ng-content />
}
`,
})
export class DisclosureComponent {
open = input(false);
}Whatever the parent puts between the tags is instantiated when the parent renders. Its constructors run and its ngOnInit hooks fire, and any HTTP calls it makes are sent, even while open() is false. For truly lazy content, take an ng-template as shown in the earlier chapter and render it with ngTemplateOutlet.
Pitfall 4: projecting the same content twice
A projected node exists once. You cannot repeat <ng-content /> in a template, for example inside an @for, to get several copies. Use an ng-template and render it as many times as you need.
Pitfall 5: confusing who owns the content
The parent owns projected content. Its bindings resolve against the parent's class, and it is checked as part of the parent's change detection. The wrapper cannot reach into it with template references or its own fields. When the wrapper needs to know about its children, use contentChild or contentChildren. Remember that contentChildren by default finds only direct children in the projected content. Pass { descendants: true } to include nested matches.
Choosing a design
Guidelines
- Use inputs for data and projection for markup. A
titlestring should be an input, not a slot. - Prefer attribute selectors such as
[panel-title]for slots. They are easy to read and do not depend on a specific tag name. - Keep slots few. Past three or four, consider splitting the component or switching to templates.
- Always provide a default slot unless you want to drop unmatched content on purpose.
- Give optional slots fallback content so an empty slot still produces sensible output.
- Document each slot's selector next to the component, since the template is the only record of the contract.
- Treat projected children as expensive. If creating them has side effects, use
ng-template.
Exercise
A parent writes <app-disclosure [open]="false"><app-report /></app-disclosure>, and the disclosure component wraps <ng-content /> in @if (open()). The app-report component loads data in ngOnInit. Does the load happen, and how would you prevent it?
Show answer
Yes, the load happens. The parent creates app-report when it renders, whether or not the slot is displayed. To prevent it, have app-disclosure accept an ng-template through contentChild.required(TemplateRef) and render it with ngTemplateOutlet only when open() is true. The parent then wraps <app-report /> in an <ng-template>.