Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 async nested data

Tags:

angular

pipe

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.

like image 574
Pascal Avatar asked Jan 09 '17 08:01

Pascal


2 Answers

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>
like image 108
Siddharth Sharma Avatar answered Sep 21 '22 10:09

Siddharth Sharma


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>
like image 31
eko Avatar answered Sep 19 '22 10:09

eko