From my component (ex. Component), I'm trying to instantiate an Angular component (ex. CustomComponent), set some properties, and send it over to a table (ex. CustomTable) for rendering, but I keep getting [object HTMLElement]
instead of the rendered element in the table cell. Here's my setup:
Component.html
<custom-table [data]="tableData"...></custom-table>
<custom-component #rowDetailTemplate></custom-component>
Component.ts
@Input() data: Array<CustomDataSource>;
@ViewChild('rowDetailTemplate') template: ElementRef;
public tableData: Array<CustomTableData> = new Array<CustomTableData>();
...
private mapper(dataSource: CustomDataSource): CustomTableData {
var detailComponent = this.template.nativeElement;
detailComponent.phone = dataSource.phone;
var tableRow = new CustomTableData();
tableRow.textColumn = "test";
tableRow.detailComponent = detailComponent;
return tableRow;
}
CustomComponent.html
<div>
<span>{{phone}}</span>
</div>
CustomComponent.ts
@Component({
selector: `[custom-component]`,
templateUrl: 'CustomComponent.html'
})
export class CustomComponent {
@Input() phone: string;
}
CustomTable.html
<mat-table [dataSource]="dataSource">
<ng-container matColumnDef...>
<mat-cell *matCellDef="let element;">
<div [innerHTML]="element.textColumn"></div>
<div [innerHTML]="element.detailComponent"></div>
</mat-cell>
</ng-container>
</mat-table>
My text column renders fine, its just the custom-component
that isn't rendering properly.
Any suggestions?
Note that CustomTable needs to be able to accept any type of component/element in detailComponent
, not just my CustomComponent.
Instead of trying to pass the component into the table, I ended up passing the table a ComponentFactory, then the table would take care of instantiating the component from a factory and attaching it to a placeholder once the table was done loading the data (otherwise it would try to attach the component to a placeholder that doesn't exist yet).
Here is what I ended up with:
Component.html
Component.ts
CustomComponent.html
CustomComponent.ts
CustomTable.html
CustomTable.ts (the meat of the solution)