[Step1] I have an object "article" which has an array of multiple content elements. Depending on the type of the element (eg. header/image), a different template should be shown. I don't want to explicitly have an own component for this, if not necessary, so I'm guessing whether ViewChild nor ContentChild is needed?
I have looked into all of the ngTemplate related questions, but havent found a solution yet. I tried to rewrite https://stackblitz.com/edit/angular-q1y2vz from ngTemplateOutlet with dynamic value, but failed so far.
component.ts
articles: any[] = [ {
"id_seq": "1234",
"content":[
{
"id": "123456",
"order": 0,
"type": "header",
"data":
{
"text": "loremipsum"
}
},
{
"id": "234567",
"order": 1,
"type": "image",
"data":
{
"url": "http://www.google.de/image.jpg"
}
}]
}]
component.html
<ng-container *ngFor="let content of articles[0]?.content;">
<ng-container [ngTemplateOutlet]="content?.type">
</ng-container>
</ng-container>
<ng-template #header >
<p>I'm a header</p>
</ng-template>
<ng-template #image >
<p>I'm an image</p>
</ng-template>
Depending on syntax and code placement of ngTemplateOutlet, I get an error
templateRef.createEmbeddedView is not a function
or nothing is rendered at all.
If i type a static string like "header", it works. So I'm guessing, the content?.type is not the way to go. On the other hand, something like this works fine:
<accordion>
<accordion-group heading="articlecontent" [isOpen]="isFirstOpen">
<div class="accordion_no_side_margin">
<accordion-group *ngFor="let content of articles[0]?.content"
[heading]="content?.type">
{{ content?.id }}
{{ content?.order }}
{{ content?.type }}
{{ content?.data }}
</accordion-group>
</div>
</accordion-group>
</accordion>
[Step2] Ultimately I do want to merge both code snippets, so that the ng-template should be executed inside the accordion.
[Step3] Later I intend to add ngTemplateOutletContext and provide some information, but the other steps should work just fine without that.
At the moment I'm using @angular-devkit/core 8.1.3