4

I need to write a component that has a template that looks like this:

<tr>...</tr>
<tr>...</tr>

This must be used with *ngFor to create a table. The selector for the component is spot-row. The component has one input variable named spot.

The desired output must look like this:

<table>
<tbody>
<tr>...</tr>
<tr>...</tr>
</tbody>
<table>

I tried the following:

<table>
<tbody>
<spot-row *ngFor="let spot of spots" [spot]="spot"></spot-row>
</tbody>
<table>

But this produced

<table>
<tbody>
<spot-row>
<tr>...</tr>
<tr>...</tr>
</spot-row>
</tbody>
<table>

Then I tried switching the component selector to [spot-row] and using ng-container

<table>
<tbody>
<ng-container spot-row *ngFor="let spot of spots" [spot]="spot"></ng-container>
</tbody>
<table>

But this produced an error

Error: Error in ./SpotRowComponent class SpotRowComponent - inline template:0:0 caused by: Failed to execute 'appendChild' on 'Node': This node type does not support this method

Then I tried template

<table>
<tbody>
<template spot-row *ngFor="let spot of spots" [spot]="spot"></template>
</tbody>
<table>

But this also gives me an error

Error: Template parse errors: Components on an embedded template: SpotRowComponent (" [ERROR ->] ")

I searched StackOverflow and found

Community
  • 1
  • 1
wertzui
  • 5,148
  • 3
  • 31
  • 51

1 Answers1

3

Suggestion as per comment, not tested.

Component selector: [spot-row]

Component template:

<tr>...</tr>
<tr>...</tr>

HTML:

<table>
   <tbody *ngFor="let spot of spots" spot-row [spot]="spot"></tbody>
<table>

This should produce:

<table>
   <tbody>
     <tr>...</tr>
     <tr>...</tr>
   </tbody>
   <tbody>
     <tr>...</tr>
     <tr>...</tr>
   </tbody>
   ...
</table>

Which is valid (multiple <tbody> in <table>).

Arg0n
  • 8,283
  • 2
  • 21
  • 38