0Pricing
Angular Academy · Lesson

TemplateRef and ViewContainerRef

Render templates dynamically.

TemplateRef and ViewContainerRef is a free Angular Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Imperative Rendering

Sometimes you must render content programmatically rather than declaratively. Angular exposes TemplateRef and ViewContainerRef for this low-level control.

TemplateRef

A TemplateRef is a handle to an <ng-template>. It represents markup you can instantiate later.

tpl = viewChild.required<TemplateRef<unknown>>('tpl');

ViewContainerRef

A ViewContainerRef is a location in the DOM where you can attach views. Inject it to control what gets rendered there.

private vcr = inject(ViewContainerRef);

createEmbeddedView

Combine the two: createEmbeddedView instantiates a TemplateRef into a ViewContainerRef.

this.vcr.createEmbeddedView(this.tpl());

Passing Context

You can pass a context object as the second argument, accessible via let- variables.

this.vcr.createEmbeddedView(this.tpl(), {
  $implicit: 'Hello',
  count: 3
});

Clearing Views

ViewContainerRef.clear() removes all views it created. This is how you toggle dynamic content on and off.

this.vcr.clear();

A Custom Structural Directive

This pattern powers custom structural directives. The directive injects both refs and renders conditionally.

@Directive({ selector: '[appUnless]' })
export class UnlessDirective {
  private tpl = inject(TemplateRef<unknown>);
  private vcr = inject(ViewContainerRef);
}

Reacting to Input

A setter on the directive input drives create/clear so the view appears or disappears.

@Input() set appUnless(condition: boolean) {
  if (!condition) {
    this.vcr.createEmbeddedView(this.tpl);
  } else {
    this.vcr.clear();
  }
}

Inserting at an Index

createEmbeddedView accepts an index to control insertion order among sibling views.

this.vcr.createEmbeddedView(this.tpl(), {}, 0);

createComponent

ViewContainerRef can also instantiate components dynamically with createComponent, the modern replacement for ComponentFactoryResolver.

const ref = this.vcr.createComponent(WidgetComponent);
ref.setInput('title', 'Hi');

Cleaning Up

Views created imperatively must be cleaned up. Destroying the host component clears its ViewContainerRef automatically, but call clear() when toggling manually.

Quick Check

Test your understanding of imperative rendering.

Recap

You learned that TemplateRef is a handle to template markup, ViewContainerRef is a DOM insertion point, and createEmbeddedView renders one into the other. Together they power custom structural directives and dynamic components.

Frequently asked questions

Is the “TemplateRef and ViewContainerRef” lesson free?

Yes — the full text of “TemplateRef and ViewContainerRef” is free to read here on the web, and the Angular Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Angular Academy course, upgrade to CoddyKit PRO.

What will I learn in “TemplateRef and ViewContainerRef”?

Render templates dynamically. You practise Angular Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Angular Academy?

No prior experience is required. Angular Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “TemplateRef and ViewContainerRef” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Angular Academy lesson?

Yes. Every Angular Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Single-Slot Content Projection
  2. Multi-Slot Projection with select
  3. ng-template and ng-container
  4. TemplateRef and ViewContainerRef
← Back to Angular Academy