Angular Academy · Lesson

TemplateRef and ViewContainerRef

Render templates dynamically.

Lesson 4 of 413 steps

TemplateRef and ViewContainerRef is a free Angular Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Angular Academy learning path, and your progress syncs across the web and the CoddyKit app. The Angular Academy course includes 4 lessons in total.

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.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
38
Lessons
144

Frequently Asked Questions

Is the “TemplateRef and ViewContainerRef” lesson free?

Yes — any 3 lessons of the Angular Academy learning path, including “TemplateRef and ViewContainerRef”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Angular Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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