Sub topic 11
Projecting nodes into dynamically created components
This chapter assumes you know ng-content slots, select, and ViewContainerRef. It covers one case: a component created in code, not written in a template, still needs content between its tags.
The problem
In a template you write <app-card><p>Hi</p></app-card>, and Angular projects the <p> for you. When you create the component with createComponent or ViewContainerRef.createComponent, no such template exists. There are no "tags" to put content between.
The projectableNodes option fills that gap. You pass DOM nodes, and Angular places them in the new component's ng-content slots.
The shape of projectableNodes
projectableNodes is an array of arrays of DOM nodes (Node[][]). Each inner array fills one ng-content slot, in the order the ng-content elements appear in the component's template.
| Index | Fills |
|---|---|
| `0` | The first `ng-content` in the template |
| `1` | The second `ng-content` in the template |
| and so on | Later slots |
Nodes are matched to slots by position. The select attribute is not evaluated for them. An array for a slot that does not exist is ignored, and a slot with no array renders its fallback content.
A worked example
This uses the single-slot CardComponent from earlier in the tutorial. The host builds the content from an ng-template, creates the card, and hands over the template's root nodes.
import {
ApplicationRef,
Component,
EmbeddedViewRef,
TemplateRef,
ViewContainerRef,
ComponentRef,
inject,
viewChild,
} from '@angular/core';
import { CardComponent } from './card.component';
@Component({
selector: 'app-host',
template: `
<button (click)="show()">Show card</button>
<button (click)="hide()">Hide card</button>
<ng-container #outlet />
<ng-template #content><p>Projected from code</p></ng-template>
`,
})
export class HostComponent {
private appRef = inject(ApplicationRef);
outlet = viewChild.required('outlet', { read: ViewContainerRef });
content = viewChild.required<TemplateRef<unknown>>('content');
private card?: ComponentRef<CardComponent>;
private view?: EmbeddedViewRef<unknown>;
show() {
if (this.card) return;
this.view = this.content().createEmbeddedView({});
this.appRef.attachView(this.view);
this.card = this.outlet().createComponent(CardComponent, {
projectableNodes: [this.view.rootNodes],
});
}
hide() {
this.card?.destroy();
if (this.view) {
this.appRef.detachView(this.view);
this.view.destroy();
}
this.card = undefined;
this.view = undefined;
}
}The steps are:
createEmbeddedViewinstantiates the template. Its bindings and directives are live, but the view is not yet in the DOM.rootNodesreturns the template's top-level DOM nodes. Here that is the<p>element.projectableNodes: [rootNodes]puts those nodes in slot 0 of the card.outlet().createComponentinserts the card next to the outlet.
Why attachView and cleanup matter
An embedded view that is not inside a ViewContainerRef is not checked by change detection. Without appRef.attachView(view), interpolations in the projected template render once and never update. Attaching the view fixes that.
Cleanup is also your job here. Destroying the card does not destroy a view that you created separately. Call detachView and destroy on it, as hide() does, or its listeners and bindings stay alive.
Plain DOM nodes
You can also project nodes that Angular does not manage:
const text = document.createElement('p');
text.textContent = 'Plain DOM';
this.outlet().createComponent(CardComponent, { projectableNodes: [[text]] });These nodes have no bindings, directives, or change detection. They are fine for static content. Use an embedded view whenever the content needs Angular features.
Without a ViewContainerRef
The standalone createComponent function works the same way. It also needs an environmentInjector and, usually, a hostElement:
import { createComponent, EnvironmentInjector, ApplicationRef, inject } from '@angular/core';
const injector = inject(EnvironmentInjector);
const appRef = inject(ApplicationRef);
const ref = createComponent(CardComponent, {
environmentInjector: injector,
projectableNodes: [[document.createTextNode('Hello')]],
});
appRef.attachView(ref.hostView);
document.body.appendChild(ref.location.nativeElement);Here you also attach the component's host view and insert its element yourself.
Common mistakes
- Passing a flat array such as
[node]instead of[[node]]. Angular expects one inner array per slot. - Counting slots by
selectname instead of by order in the template. - Forgetting
attachView, which freezes bindings in the projected template. - Forgetting to destroy the extra embedded view.
Exercise
A component's template contains <ng-content select="[title]" /> followed by <ng-content />. You create it with projectableNodes: [[bodyNode]]. Which slot receives bodyNode, and what does the default slot show?
Show answer
bodyNode goes into the first slot, [title], because nodes are matched by position and select is not evaluated. The default slot receives no array, so it renders nothing, or its fallback content if it has any. To fill the default slot, pass a second array: [[titleNode], [bodyNode]].