Sub topic 3
Named slots with select
This chapter assumes you can write an Angular component with an inline template and that you know basic CSS selectors (element, class, attribute). You should also have seen a single <ng-content /> slot, which the previous chapter covered.
Why one slot is not enough
A single <ng-content /> dumps everything the parent provides into one place. Many components need structure. A panel has a title, a body and some actions, and each belongs in a different part of the layout. The select attribute on ng-content lets you define several named slots. Each slot claims the projected nodes that match its CSS selector.
Defining slots
import { Component } from '@angular/core';
@Component({
selector: 'app-panel',
template: `
<header><ng-content select="[panel-title]" /></header>
<section><ng-content /></section>
<footer><ng-content select="[panel-actions]" /></footer>
`,
})
export class PanelComponent {}The component has three slots. The first takes elements with a panel-title attribute. The last takes elements with a panel-actions attribute. The plain <ng-content /> in the middle takes whatever is left over. The next chapter covers that default slot in detail.
Filling slots from the parent
The parent marks each child with an attribute that matches a selector.
<app-panel>
<h2 panel-title>Settings</h2>
<p>This paragraph goes to the default slot.</p>
<button panel-actions>Save</button>
</app-panel>The order of the children in the parent does not matter. If you put the <button> first, it still renders inside <footer>, because placement follows the slot that matches, not the source order.
What a selector can match
The value of select is a CSS selector, but only a subset is supported. The following table shows the forms you will use most.
| Selector | Matches | Example in the parent |
|---|---|---|
| `h2` | Element name | `<h2>Title</h2>` |
| `.title` | Class | `<div class="title">` |
| `[panel-title]` | Attribute | `<span panel-title>` |
| `h2.title` | Element with class | `<h2 class="title">` |
| `:not([panel-title])` | Negation | Any node without the attribute |
Attribute selectors are the usual choice. They work on any element, and they state the intent of the slot clearly. A selector on an element name such as h2 is shorter, but it breaks if the consumer wants a <h3> instead.
Rules that surprise people
Three rules explain most of the bugs you will see with named slots.
- Matching happens at compile time, on the parent's template. Angular decides which slot each node goes to while compiling the parent. A binding such as
[attr.panel-title]="''"does not make a node match, because the attribute does not exist at compile time. Write the attribute as plain static text. - Only top-level nodes are matched. A selector tests the direct children of the component's tag in the parent template. It does not look inside them. In the example below, the
<h2>is nested in a<div>. The<div>is the only top-level node, so the whole<div>goes to the default slot and the<h2>never reaches the header.
``html <app-panel> <div> <h2 panel-title>Settings</h2> </div> </app-panel> ``
- The first matching slot wins. If a node matches the selectors of two slots, Angular uses the first one in the component's template. Keep your selectors mutually exclusive so the result does not depend on slot order.
If you need to place a group of nodes in one slot without adding a wrapper element, Angular offers ngProjectAs on an ng-container. A later chapter covers it.
Choosing slot names
Name slots after their role, not their look. [panel-actions] still makes sense if the buttons move to the top of the panel. [bottom-right] does not. Prefer a shared prefix such as panel-, so a reader of the parent template can see which component owns the attribute.
Exercise
A component template contains <ng-content select=".badge" /> followed by <ng-content />. The parent writes <app-card><div><span class="badge">New</span></div></app-card>. Where does the badge render, and how would you change the parent so it appears in the first slot?
Show answer
The badge renders in the default slot. The only top-level node is the <div>, and it has no badge class, so select=".badge" does not match it. Selectors do not look at nested nodes. To fix it, move the class onto the top-level node, for example <span class="badge">New</span> as a direct child of <app-card>.