Content Projection, ViewChild and Template Outlets

Angular 15 min min read Updated: Nov 16, 2026 Intermediate
Content Projection, ViewChild and Template Outlets
Intermediate Topic 7 of 7

Content Projection with ng-content

Sometimes a component should be a frame and the parent decides what goes inside, like a card, a modal or a tab panel.

typescript @Component({ selector: 'app-card', template: ` <div class="card"> <header><ng-content select="[card-title]" /></header> <div class="body"><ng-content /></div> <footer><ng-content select="[card-footer]" /></footer> </div> `, styles: `.card { border: 1px solid #ddd; border-radius: 8px; padding: 12px; }`, }) export class Card {}
html <app-card> <h3 card-title>Account</h3> <p>Your plan renews on 1 December.</p> <button card-footer>Manage</button> </app-card>
SlotReceives
<ng-content />Everything not matched elsewhere (default slot)
<ng-content select="[card-title]" />Elements with that attribute
select="h3" / select=".class"By tag or class

ViewChild: Access Elements or Child Components

typescript import { Component, viewChild, ElementRef, afterNextRender } from '@angular/core'; @Component({ selector: 'app-search', template: `<input #box placeholder="Search"> <button (click)="focus()">Focus</button>`, }) export class Search { box = viewChild.required<ElementRef<HTMLInputElement>>('box'); focus() { this.box().nativeElement.focus(); } }

viewChild() returns a signal. viewChild.required throws an error if the element is missing. For several elements use viewChildren().

typescript // access a child component child = viewChild(ProductCard); refresh() { this.child()?.reload(); } // projected content tabs = contentChildren(Tab);
Prefer data over DOM access: If you can solve a problem with signals and bindings, do that. Use ElementRef only for focus, measuring size or third-party libraries.

ng-container

<ng-container> is an invisible grouping element that does not add anything to the DOM.

html <ng-container> @if (user) { <p>{{ user.name }}</p> } </ng-container>

ng-template and ngTemplateOutlet

<ng-template> is a block of HTML that is not shown until you render it. You can pass templates to child components for flexible lists.

typescript @Component({ selector: 'app-list', imports: [NgTemplateOutlet], template: ` @for (item of items(); track $index) { <ng-container [ngTemplateOutlet]="rowTemplate() ?? defaultRow" [ngTemplateOutletContext]="{ $implicit: item, index: $index }" /> } <ng-template #defaultRow let-item>{{ item }}</ng-template> `, }) export class ListComp { items = input<any[]>([]); rowTemplate = contentChild<TemplateRef<any>>('row'); }
html <app-list [items]="users"> <ng-template #row let-user let-i="index"> <strong>{{ i + 1 }}.</strong> {{ user.name }} ({{ user.email }}) </ng-template> </app-list>
Where is this useful? Data tables with custom cell templates, dropdowns with custom option layouts and any component library. Angular Material and CDK use this idea heavily.

Conclusion

ng-content lets parents supply content, viewChild reads elements from your own template, and ng-template with ngTemplateOutlet lets you pass whole templates for reusable components.

Quick Summary: ng-content = slots; viewChild = query; ng-template + outlet = pass templates.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators