I have a Angular2 Component displaying some data in a table.
export class DiagnosisComponent {
headers: Array<String> = ["hello world", "hello world"];
table: Observable<TableData>;
constructor(private eventService: EventService) {
this.table = this.eventService.getDiagnosisEvents();
this.table.subscribe(console.log.bind(console));
}
}
And here is the TableData class
export class TableData {
content: Array<any>;
paging: Paging;
constructor(obj: any) {
this.paging = {
size: obj.size,
totalElements: obj.totalElements,
currentPage: 1
};
this.content = obj.content;
}
}
export interface Paging {
size: number;
totalElements: number;
currentPage: number;
}
Now I want to bind the table.content Array to a *ngFor with a async pipe. My problem is that i need to get the nested data, and not the TableData itself.
<div class="row">
<vpscout-filter></vpscout-filter>
<table class="table">
<thead>
<tr><th *ngFor="let header of headers">{{header | translate}}</th></tr>
</thead>
<tbody>
<tr *ngFor="let row of table | async ">
<td>test</td>
</tr>
</tbody>
</table>
</div>
Changing the *ngFor to <tr *ngFor="let row of table.content | async ">
does not work.
I solved a similar problem like this:
<tr *ngFor="let row of (table | async)?.content ">
<td>test</td>
</tr>
Since, I'm using immutableJs for my project and converting the observable data to map, the exact solution that worked for me was:
<tr *ngFor="let row of (table | async)?.get('content') ">
<td>test</td>
</tr>
You can create a new field for the content:
this.table.subscribe((data)=>{
this.tableContent = data.content
});
and bind that new field to the *ngFor
<tbody *ngIf="tableContent">
<tr *ngFor="let row of tableContent">
<td>test</td>
</tr>
</tbody>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With