Sub topic 5
Grouping content with ng-container and ngProjectAs
This chapter assumes you know how to write an Angular component and its template, how ng-content with select routes content into named slots, and basic CSS selectors. If select is unfamiliar, read the previous chapter first.
The problem: one slot, several nodes
The app-panel component from the last chapter has a title slot defined as <ng-content select='[panel-title]' />. Suppose the parent wants two elements in that slot: a heading and a small "beta" label.
You could mark each element with the panel-title attribute. That works, but it repeats the attribute. A second option is to wrap both elements in a div panel-title. That adds a div to the DOM, which can break your CSS, for example a flex layout or a header > h2 selector.
ng-container avoids the wrapper. It is an Angular element that groups nodes but renders nothing in the DOM. It cannot take a plain attribute like panel-title as a reliable match, though, because select is matched against the template at compile time, and the next section explains what the matcher does with ng-container.
Why select does not match an ng-container by itself
The select value is a CSS selector. Angular compares it with the element name and attributes in the parent's template. An ng-container has the name ng-container. If you write this, the container is not matched by [panel-title] in the way you might hope:
<app-panel>
<ng-container panel-title>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
</app-panel>Plain attributes are not a dependable way to route a container, so Angular provides a dedicated attribute for the job.
Using ngProjectAs
ngProjectAs tells the compiler to treat an element as if it matched a given selector, for the purpose of choosing a slot only. The value is a CSS selector.
<app-panel>
<ng-container ngProjectAs='[panel-title]'>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
<p>Body text</p>
</app-panel>With the PanelComponent from the previous chapter, this renders as follows:
<header>
<h2>Settings</h2>
<small>beta</small>
</header>
<section>
<p>Body text</p>
</section>
<footer></footer>The container leaves no trace. Its two children sit directly inside header. The paragraph has no matching attribute, so it goes to the default slot.
Rules to remember
ngProjectAsaccepts one selector. It does not accept a comma-separated list.- It works on any element, not only
ng-container. It is most useful onng-containerbecause that element has nothing else to match on. - It affects only slot selection. It does not add the attribute to the element, so CSS rules such as
[panel-title] { ... }in the parent will not apply. - The matching happens at compile time, so the value must be a string literal. You cannot bind it with
[ngProjectAs].
A common mistake: structural directives
Structural blocks such as @if and @for have a related issue. The compiler picks the slot for a block from its single root element. Consider this:
<app-panel>
@if (showTitle) {
<h2 panel-title>Settings</h2>
}
</app-panel>Here the block has one root element with panel-title, so the title goes to the title slot. If the block had two root elements, or a root element without the attribute, the content would fall into the default slot. When that happens, wrap the content in an ng-container with ngProjectAs.
<app-panel>
@if (showTitle) {
<ng-container ngProjectAs='[panel-title]'>
<h2>Settings</h2>
<small>beta</small>
</ng-container>
}
</app-panel>Choosing between a wrapper element and ng-container
| Situation | Better choice |
|---|---|
| One element for the slot | Put the matching attribute on that element |
| Several nodes for one slot, and the DOM must stay flat | `ng-container` with `ngProjectAs` |
| You want to style the group as one box | A real wrapper element with the attribute |
Exercise
A parent writes <ng-container ngProjectAs='[panel-actions]'><button>Save</button><button>Cancel</button></ng-container> inside app-panel. Where do the two buttons render, and how many extra DOM elements does the container add?
Show answer
Both buttons render inside the footer, because ngProjectAs makes the container match the [panel-actions] selector. The container adds zero DOM elements, so the buttons are direct children of footer.
Exercise
A developer writes <ng-container ngProjectAs='[panel-title]'> and then adds the CSS rule [panel-title] { color: red; } in the parent's stylesheet. The heading stays black. Why?
Show answer
ngProjectAs only influences which slot is chosen. It does not add a panel-title attribute to any element, and the ng-container is not in the DOM at all, so the CSS selector has nothing to match. Put a class on the heading itself instead.