Dynamic Components, ngTemplateOutlet and ViewContainerRef
ngComponentOutlet
Show a component chosen at run time with one directive.
typescript
@Component({
selector: 'app-widget-host',
imports: [NgComponentOutlet],
template: `
@for (w of widgets(); track w.id) {
<ng-container *ngComponentOutlet="w.component; inputs: w.inputs" />
}
`,
})
export class WidgetHost {
widgets = signal([
{ id: 1, component: SalesChart, inputs: { year: 2026 } },
{ id: 2, component: RecentOrders, inputs: { limit: 5 } },
]);
}
inputs: The
inputs property of ngComponentOutlet sets the input() values of the created component, and updating the object updates the inputs.
ViewContainerRef.createComponent
typescript
@Component({
selector: 'app-host',
template: `<button (click)="show()">Show alert</button><ng-container #slot />`,
})
export class Host {
slot = viewChild.required('slot', { read: ViewContainerRef });
private ref?: ComponentRef<AlertBox>;
show() {
this.slot().clear();
this.ref = this.slot().createComponent(AlertBox);
this.ref.setInput('message', 'Saved successfully'); // set an input()
this.ref.instance.closed.subscribe(() => this.ref?.destroy()); // listen to an output()
}
}
A Simple Modal Service
typescript
@Injectable({ providedIn: 'root' })
export class ModalService {
private appRef = inject(ApplicationRef);
private env = inject(EnvironmentInjector);
private doc = inject(DOCUMENT);
open<T>(component: Type<T>, inputs: Record<string, unknown> = {}) {
const ref = createComponent(component, { environmentInjector: this.env });
Object.entries(inputs).forEach(([k, v]) => ref.setInput(k, v));
this.appRef.attachView(ref.hostView);
this.doc.body.appendChild(ref.location.nativeElement);
return { close: () => { this.appRef.detachView(ref.hostView); ref.destroy(); } };
}
}
// usage
const modal = this.modals.open(ConfirmDialog, { message: 'Delete this item?' });
CDK Dialog: For production modals,
@angular/cdk/dialog or Angular Material's MatDialog already handle focus, overlays and accessibility.
ngTemplateOutlet: Reuse Template Pieces
html
<ng-template #itemTpl let-item let-index="index">
<li>{{ index + 1 }}. {{ item.name }}</li>
</ng-template>
<ul>
@for (p of products(); track p.id; let i = $index) {
<ng-container *ngTemplateOutlet="itemTpl; context: { $implicit: p, index: i }" />
}
</ul>
A List Component with a Custom Row Template
typescript
@Component({
selector: 'app-smart-list',
imports: [NgTemplateOutlet],
template: `
@for (item of items(); track $index) {
@if (rowTpl(); as tpl) {
<ng-container *ngTemplateOutlet="tpl; context: { $implicit: item }" />
} @else {
{{ item }}
}
}
`,
})
export class SmartList<T> {
items = input.required<T[]>();
rowTpl = contentChild(TemplateRef);
}
html
<app-smart-list [items]="users()">
<ng-template let-user>
<strong>{{ user.name }}</strong> ({{ user.role }})
</ng-template>
</app-smart-list>
Clean up: Components created manually must be destroyed with
destroy() (or removed from the container), or they remain in memory.
Conclusion
Use ngComponentOutlet for simple dynamic components, createComponent for full control and ngTemplateOutlet with context to reuse and customize pieces of templates.
Quick Summary: ngComponentOutlet / createComponent for dynamic components; ngTemplateOutlet + context for templates.

