Sub topic 5
Grouping content with ng-container and ngProjectAs
This chapter assumes you can build a standalone Angular component, you know the select attribute of ng-content from the previous chapters, and you can read a CSS attribute selector such as [panel-title]. After it, you will be able to send several sibling nodes into one named slot without adding a wrapper element to the DOM.
The problem: one slot, several nodes
The select attribute of ng-content is matched against the top-level nodes of the projected content. These are the nodes that sit directly between the opening and closing tags of your component in the parent's template. Angular does not look inside those nodes for matches.
Here is the panel component from the previous chapter, with a small host component that uses it.
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 {}
@Component({
selector: 'app-root',
imports: [PanelComponent],
template: `
<app-panel>
<h2 panel-title>Settings</h2>
<p>This paragraph goes to the default slot.</p>
<button panel-actions>Save</button>
</app-panel>
`,
})
export class AppComponent {}This works because each marked element is a direct child of app-panel. Suppose the title now needs two elements: a heading and a "beta" badge. Marking both with panel-title works, but it spreads the slot name across every node. Wrapping them in a div that carries panel-title also works, but it adds a real element to the DOM. That div can break a flex or grid layout in the panel's header, or add unwanted margins and selector depth.
ng-container: grouping without an element
ng-container is an Angular template element that groups nodes but renders no element of its own. Its children appear in the DOM, and the container leaves only a comment node behind.
Because ng-container is not a real element, a plain tag name or attribute gives Angular little to match on. The fix is the ngProjectAs attribute, which tells Angular what selector to pretend the node matches.
<app-panel>
<ng-container ngProjectAs='[panel-title]'>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
<p>Body text</p>
</app-panel>Angular matches the ng-container against the panel's slots as if it were an element with the attribute panel-title. It matches the first slot, so both children are projected into the header. The rendered DOM looks like this:
<header>
<!--ng-container-->
<h2>Settings</h2>
<small>beta</small>
</header>There is no wrapper element. The h2 and small are direct children of header, so your CSS can target them as such.
What ngProjectAs does and does not do
ngProjectAs affects only the decision about which slot receives the node. It does not change the node, add an attribute to the DOM, or alter how the node is created.
You can put ngProjectAs on any element, not only on ng-container. This is useful when you project a component whose host element you cannot easily mark, or when you want to override what a node would otherwise match.
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class='badge'>beta</span>`,
})
export class BadgeComponent {}
@Component({
selector: 'app-panel-demo',
imports: [BadgeComponent],
template: `
<app-panel>
<app-badge ngProjectAs='[panel-actions]' />
<p>Body text</p>
</app-panel>
`,
})
export class PanelDemoComponent {}Without ngProjectAs, app-badge has the tag name app-badge and no attributes. It matches no slot selector, so it would land in the default slot. With it, the badge appears in the footer.
The following table summarizes your options for putting more than one node into a named slot.
| Approach | Extra DOM element | Slot name repeated | Notes |
|---|---|---|---|
| Mark every node with the slot attribute | No | Yes, on each node | Simple for two nodes, tedious for more. |
| Wrap the nodes in a `div` that carries the attribute | Yes | No | Can disturb layout and CSS selectors. |
| `ng-container` with `ngProjectAs` | No | No | Best choice when the nodes must stay siblings. |
Rules and gotchas
ngProjectAs takes one selector
The value of ngProjectAs is a single CSS selector, such as [panel-title], app-footer, or h2.title. Comma-separated selector lists are not something to rely on here. If a node must match two slots, project it into one and rethink the component's API.
The value is static
Write ngProjectAs='[panel-title]' as a plain attribute. A binding such as [ngProjectAs]='slotName' is not supported, because Angular must decide the slot when it compiles the template, not at run time. If the destination must change while the app runs, you need a different tool, such as ng-template with ngTemplateOutlet, which a later chapter covers.
Only top-level nodes are matched
ngProjectAs does not make Angular search deeper. The attribute must be on the node that sits directly inside your component's tags. In this example the heading is nested one level down, so the slot attribute is never seen:
<app-panel>
<section>
<h2 panel-title>Settings</h2>
</section>
</app-panel>Angular sees one top-level node, the section. It matches no slot selector, so the whole section, heading included, goes to the default slot. The fix is to move the marker to the top level, either on the section itself or by replacing the section with an ng-container that has ngProjectAs.
An unmarked ng-container goes to the default slot
If you write an ng-container with no ngProjectAs and no matching attribute, it is a top-level node that matches no named slot. It and its children are projected into the default ng-content. If the component has no default slot, they are not rendered at all. That is a common cause of "my content disappeared" bugs.
Nodes disappear when no slot accepts them
Projection has no error for unmatched content. If a node matches no select and the component has no catch-all <ng-content />, Angular does not display the node. Nothing is logged. When a projected node is missing, check these things in order:
- Does the node match a
selectselector at the top level? - If it does not, does the component declare a default slot?
- If it is inside a wrapper, should the wrapper be an
ng-containerwithngProjectAs?
Structural directives and control flow
When you put *ngIf or *ngFor on an element, Angular turns it into an ng-template. It copies the element's attributes so that slot matching still works. So <h2 panel-title *ngIf='ready'> still reaches the title slot.
If you want to show or hide a group of nodes, the clearest approach is to put the condition on an ng-container and add ngProjectAs there. That way the slot choice is explicit and does not depend on how the compiler treats a particular control flow construct.
<app-panel>
<ng-container *ngIf='showBeta' ngProjectAs='[panel-title]'>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
</app-panel>Here NgIf must be listed in the host component's imports, because standalone components import everything they use. The ngProjectAs attribute stays on the container, so the group is routed to the header whenever the condition is true.
Selectors can target tag names too
select is not limited to attributes. A slot declared as <ng-content select='app-footer' /> accepts a node with the tag app-footer. You can use ngProjectAs='app-footer' to send an ng-container there. Attribute selectors are usually easier to read in the parent's template, while tag names suit components that are designed to be used as a set.
A slightly larger example
The following component pair uses a grouped title and grouped actions in one card. It is complete and can be pasted into a standalone Angular project.
import { Component } from '@angular/core';
@Component({
selector: 'app-card',
template: `
<article class='card'>
<header class='card-head'>
<ng-content select='[card-title]' />
</header>
<div class='card-body'>
<ng-content />
</div>
<footer class='card-actions'>
<ng-content select='[card-actions]' />
</footer>
</article>
`,
styles: `
.card-head { display: flex; align-items: baseline; gap: 0.5rem; }
.card-actions { display: flex; gap: 0.5rem; }
`,
})
export class CardComponent {}
@Component({
selector: 'app-root',
imports: [CardComponent],
template: `
<app-card>
<ng-container ngProjectAs='[card-title]'>
<h2>Invoice 1042</h2>
<small>overdue</small>
</ng-container>
<p>Amount due: 120.00 EUR</p>
<ng-container ngProjectAs='[card-actions]'>
<button type='button'>Send reminder</button>
<button type='button'>Mark paid</button>
</ng-container>
</app-card>
`,
})
export class AppComponent {}The header uses display: flex with a gap. The h2 and small are direct children of the header, so the gap applies between them. If you had wrapped them in a div, the flex container would see one child and the gap would do nothing. Avoiding that kind of surprise is the main reason to choose ng-container here.
Design guidance
- Prefer a simple marker attribute on a single element when a slot takes one node. Use
ng-containerwithngProjectAsonly when a slot takes a group. - Document each slot's selector next to the component, since callers must know the exact string to use in
ngProjectAs. - Keep slot selectors simple. An attribute selector such as
[card-title]is easy to type and to search for. Compound selectors makengProjectAsvalues harder to get right. - Remember that
ngProjectAsonly changes routing. The projected nodes are still created and bound by the parent, which the chapter on ownership explains.
Exercises
Exercise
A card component has the slot <ng-content select='[card-actions]' /> and no default <ng-content />. The parent writes <app-card><div><button card-actions>OK</button></div></app-card>. Where does the button end up, and how would you fix it so the button appears in the actions slot without keeping the div?
Show answer
The button is not rendered anywhere. The only top-level node is the div, which has no card-actions attribute and matches no slot. With no default slot, it is dropped along with its button. Fix it by replacing the div with <ng-container ngProjectAs='[card-actions]'> and keeping the button inside it, or by moving the card-actions attribute to the div.
Exercise
Why can't you write [ngProjectAs]='slotName' to choose a slot at run time, and what other technique would you consider for that need?
Show answer
Angular decides which slot receives a node when the template is compiled, so ngProjectAs must be a static attribute value. For run-time choices, pass an ng-template and render it with ngTemplateOutlet where you need it, or restructure the component's inputs.
Exercise
A header slot uses display: flex; gap: 0.5rem. You project a heading and a badge into it, once with a wrapping div and once with ng-container ngProjectAs. Which version shows the gap between the heading and the badge, and why?
Show answer
The ng-container version. It renders no element, so the heading and badge are both direct children of the flex container and the gap applies between them. With the div, the flex container has one child, so the gap has no effect between the heading and the badge.