Quick Notes: Using ng-template with ng-container
Learn how to pass data to ng-template

I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
Search for a command to run...
Learn how to pass data to ng-template

I am a developer from Toronto, Canada and I enjoy writing about JavaScript, Angular and the .NET framework.
No comments yet. Be the first to comment.
What does 99.99% mean?

Using Projections - getting related data

Expose an entity using HotChocolate

Reactive programming is somewhat of a shift in thinking on how you get values from a source of data. Instead of you asking the source every few seconds for an update, the source sends you data you are interested in over time. In other words, we are t...

Learn to create your own tags like control

ng-template element does not display it's content to the browser. Instead, you can use ng-container and pass it the template to use. ng-container in turn, renders the template.
Here is an example of ng-container displaying the greeting ng-template:
<ng-template #greeting>
Hello World
</ng-template>
<ng-container [ngTemplateOutlet]="greeting"></ng-container>
The input [ngTemplateOutlet] refers to the variable pointing to the template to be used.
One of the benefits of ng-template is that it allows us to define variables that it can display. There are two types of variables that can be passed - $implicit and an explicit one. Below is an example that passes both:
<ng-template #greeting let-name let-country="birthPlace">
{{name}} lives in {{county}}
</ng-template>
<ng-container [ngTemplateOutlet]="greeting" [ngTemplateOutletContext]="data"></ng-container>
Notice that ng-template has two let-* properties:
ng-container has another property passed to it - the context object, through the input property named ngTemplateOutletContext.
Here is the typescript file with the context object named data:
data = {
$implicit: 'Mark',
birthPlace: 'Canada'
}
Remember, if you don't assign a value to the let-* property, it will pick up value from $implicit within your context. Context must be an object, otherwise nothing will be displayed (empty value on the template).