Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsIn Angular, use bindings and control-flow blocks for changing values and repeated or conditional UI, NgTemplateOutlet to render a reusable template fragment, and NgComponentOutlet when the selected unit is a component. For code-driven creation, use ViewContainerRef. The right choice depends on what must vary: markup, a component, or the logic that decides when and how to create a view.
Start with bindings and control flow
Most dynamic interfaces do not need a separately created view. Angular templates can display component state, respond to events, and show or repeat content as that state changes. Interpolation displays a value, square brackets bind a DOM property, the attr. prefix binds an HTML attribute, and parentheses register an event listener.
As an Amazon Associate I earn from qualifying purchases.
<p>{{ message() }}</p>
<button [disabled]="isSaving()" (click)="save()">Save</button>
@if (isReady()) {
<p>Ready</p>
} @else {
<p>Loading</p>
}
@for (item of items(); track item.id) {
<p>{{ item.name }}</p>
}
Here, the template reads signals; when their values change, Angular updates the affected UI. In an @for block, the track expression associates data items with rendered DOM elements. Use the control-flow syntax supported by your project’s Angular version. See Angular’s templates guide for bindings and control flow.
Render a reusable template fragment
Declare the fragment and choose where it appears
An <ng-template> declares a fragment; Angular does not display its contents just because the declaration exists. Angular represents the fragment as a TemplateRef. Give it a template reference, then pass that reference to NgTemplateOutlet where the fragment should render:
#1 Best Overall
<ng-template #notice>
<p>Your changes are saved.</p>
</ng-template>
<ng-container *ngTemplateOutlet="notice"></ng-container>
The outlet is declarative: it places the prepared fragment in the view. <ng-container> provides a location without adding a wrapper element to the DOM. This is useful when choosing between a small set of alternate fragments or reusing a fragment in different locations. Angular documents these APIs in its template fragments guide and ng-container guide.
Pass data through a context object
Use a context object when the fragment needs values from the rendering location. A let- declaration creates a local variable in the fragment. The context object’s $implicit property supplies the default local value; named keys can be mapped explicitly.
Rank #2
<ng-template #row let-item let-position="position">
<p>{{ position }}: {{ item.name }}</p>
</ng-template>
<ng-container
*ngTemplateOutlet="row; context: { $implicit: selectedItem, position: 1 }">
</ng-container>
In this example, item receives $implicit, while position receives the context’s position key. Keep the context keys and let- declarations aligned so the fragment’s data contract is clear.
Recommended Free Tools
Understand declaration and injector context
A fragment’s expressions evaluate against the component where the fragment was declared, even when an outlet renders it elsewhere. By default, its dependency-injection context also follows the declaration site. If the fragment needs a different injector, supply one explicitly; NgTemplateOutlet also documents the 'outlet' option for inheriting the injector at the insertion location, which can be useful with recursive structures or outlet-scoped providers. The template fragments guide describes the outlet context and injector options.
Rank #3
Choose a template or a component
| Need | Use | Data and context |
|---|---|---|
| Render alternate or reusable markup | NgTemplateOutlet with a TemplateRef |
A context object supplies values to let- locals; bindings still use the declaration component’s context. |
| Select a unit with its own component view and behavior | NgComponentOutlet with a component type |
Pass an inputs object and, where needed, injector or environment injector configuration. |
| Create a view imperatively from TypeScript | ViewContainerRef |
For a fragment, call createEmbeddedView(templateRef, context); for dynamic components, use the programmatic rendering APIs. |
Use a fragment when the thing that changes is a prepared piece of template content. Use a component when the selected unit should own component behavior, inputs, or its own view. The distinction is about the unit being selected, not a claim that one approach is universally faster.
Render a component dynamically
NgComponentOutlet is the declarative option when the component type itself varies. It instantiates that component in the current view and supports an inputs object, injector and environment injector, projected content, and NgModule configuration.
Rank #4
<ng-container *ngComponentOutlet="selectedComponent; inputs: componentInputs">
</ng-container>
Here, selectedComponent is the component type chosen by the application, and componentInputs supplies its inputs. Consult the NgComponentOutlet API reference for the available configuration. If TypeScript needs imperative control over creation, use ViewContainerRef and the programmatic rendering APIs in Angular’s programmatic rendering guide.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Use programmatic embedded views when code must control creation
For a template fragment, ViewContainerRef.createEmbeddedView creates an embedded view from a TemplateRef and optional context. This is useful when creation or placement is driven by imperative TypeScript logic rather than a declarative outlet.
viewContainerRef.createEmbeddedView(templateRef, {
$implicit: selectedItem,
position: 1,
});
Choose this route when code must decide when to create the view or manage it through a view container. For a declarative fragment outlet, prefer NgTemplateOutlet; for a component selected at runtime, use NgComponentOutlet or the programmatic component APIs. The template fragments guide and programmatic rendering guide cover these approaches.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




