I have been spending more time trying to understand the following blog post Creating Reusable Components with NgTemplateOutlet in Angular
The working code of the post above can be found at stackblitz.
In the UsageExample
component the card-or-list-view component is called. The items and mode input parameters are supplied which I understand very well.
Now what I don't understand is how
<ng-container *cardItem="let item">
<h1>{{item.header}}</h1>
<p>{{item.content}}</p>
</ng-container>
<span *listItem="let item">
<h1>{{item.header}}</h1>
<p>{{item.content}}</p>
</span>
in the UsageExample
template replaces
<ng-container *ngSwitchCase="'card'">
<div *ngFor="let item of items" style="margin: 5px;border: black 1px solid">
<ng-container *ngTemplateOutlet="cardItemTemplate; context: {$implicit: item}">
</ng-container>
</div>
</ng-container>
<ul *ngSwitchCase="'list'">
<li *ngFor="let item of items">
<ng-container *ngTemplateOutlet="listItemTemplate; context: {$implicit: item}"></ng-container>
</li>
</ul>
in the template of CardOrListViewComponent
component. In the CardOrListViewComponent
component, two directives are declared
@ContentChild(CardItemDirective, {read: TemplateRef}) cardItemTemplate;
@ContentChild(ListItemDirective, {read: TemplateRef}) listItemTemplate;
and are used in its templates with *ngTemplateOutlet
.
How do these directives got replaced by
<ng-container *cardItem="let item">
<h1>{{item.header}}</h1>
<p>{{item.content}}</p>
</ng-container>
<span *listItem="let item">
<h1>{{item.header}}</h1>
<p>{{item.content}}</p>
</span>
in the UsageExample
component.
How do *cardItem
and *listItem
connect to the two @ContentChild
directives in CardOrListViewComponent
component. Their names are not even similar.