0

I have a problem with using my custom component on the HTML table, it looks like it hampers the tag

my app component HTML:

<table cellpadding="0" cellspacing="0" border="0">
     <tbody>
        <app-item-form *ngFor="let item of checklists?.items" [child]="item"></app-item-form>
     </tbody>
</table>

app-item-form.html :

<table cellpadding="0" cellspacing="0" border="0">
     <tbody>
        <app-item-form *ngFor="let item of checklists?.items" [child]="item"></app-item-form>
     </tbody>
</table>

app-item-form.ts :

@Component({
  selector: 'app-item-form',
  templateUrl: './item-form.component.html',
  styleUrls: ['./item-form.component.scss']
})
export class ItemFormComponent implements OnInit {

  @Input()
  child: ItemChecklistModel;

2 Answers 2

2

You can specify an attribute selector for the custom component such as [someSelector]. This selector can be put onto the element to retain the original tag. This pattern can be helpful in your situation as well in situations where making accessible components such as inputs would otherwise require dozens of @Input() to be specified (Angular Material Input for example).

At the base level it would look like this:

Component:

import { Component, Input } from '@angular/core';

@Component({
  selector: '[appItemForm]',
  template: `<tbody>your markup</tbody>`,
  styles: [`h1 { font-family: Lato; color: red; }`]
})
export class ItemFormComponent  {
  @Input() name: string;
}

Template:

<table appItemForm></table>

With your nested structure, you could use a combination of attribute selector component selector as well as ng-container to avoid adding invalid markup tags. Something like:

<table cellpadding="0" cellspacing="0" border="0">
     <tbody>
        <ng-container *ngFor="let item of checklists?.items">
          <table [appItemForm] [child]="child"></table>
        </ng-container>
     </tbody>
</table>

Here is an example in action.

Hopefully that helps!

Sign up to request clarification or add additional context in comments.

Comments

-1

I solved it with bootstrap classes

my app component HTML:

<div class="tbl-content" >
    <div app-item-form *ngFor="let item of checklists?.items" [child]="item"></div>
/div>

app-item-form.html :

<div class="row" >
  <div class="col">{{child.label}}</div>
  <div class="col" *ngFor="let att of child?.attrValues"></div>
</div>
  <div app-item-form *ngFor="let c of child?.childrens" [child]="c"></div>

app-item-form.ts :

@Component({
  selector: '[app-item-form]',
  templateUrl: './item-form.component.html',
  styleUrls: ['./item-form.component.scss']
})

2 Comments

Glad to hear you solved it, but this is a complete change to the structure and tags of your component. Was your intention to use a table component?
Yeah but if you noticed I need it to call my custom component inside himself and that's gonna missed up how the table looks if I used table/tr/td tags

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.