A. B
A. B

Reputation: 373

Angular, display array in array using service

I get all data json from ws like this:

{
  "StatusCode":0,
  "StatusMessage":"OK",
  "StatusDescription":   
   [
      {"homeboxpackage_id":"1",
        "active":0,
        "homebox_id":"11",
        "serial_number":"serialn1",
        "user_id":"31", 
        "sensors": { 
              "0":{
                    "sensor_serial":"sensorserial1",
                    "sensors_id":"11E805BA6732C3DB"
              }, 
              "1":{
                    "sensor_serial":"sensorserial2", 
                    "sensors_id":"11E805BA6F1E6CE9"
              }, 
              "2":{
                    "sensor_serial":"sensorserial3", 
                    "sensors_id":"11E805BA7716C775"
              }
        }
    }
  ]

In html code, I want to display data sensors in list, but dosn't show. My html code:

  <table class="bordered table-bordered" [mfData]="homeboxsp | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage"
    [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
    <thead>
      <tr>
        <th>
          <mfDefaultSorter by="serial_number">Serial Number</mfDefaultSorter>
        </th>
        <th>
          <mfDefaultSorter by="sensors">Sensor Serial</mfDefaultSorter>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let item of homeboxsp">
        <td>{{item.serial_number}}</td>
        <ul>
          <li>{{item.sensors}}</li>
        </ul>
        <td>{{item.active}}</td>
     </tr>
    </tbody>
   </table>

homeboxsp is in component.ts

 this.ws.homeboxPGetAll().subscribe(
      homeboxsp => {
        this.homeboxsp = homeboxsp; // in console display all my array in array
           }
    );

image

[1]: https://i.sstatic.net/QxRTr.png

Thank you

Upvotes: 2

Views: 9530

Answers (2)

Pramod Patil
Pramod Patil

Reputation: 2763

You need to parse response as a json in order to use actual response body. res.json will extract the body data and return it to the subscribe. Make sure you import rxjs library first.

//import { Http, Headers, RequestOptions, Response } from '@angular/http';
  //import { Observable } from 'rxjs';
  //import 'rxjs/add/operator/map';

this.ws.homeboxPGetAll().map((res:Response) => res.json()).subscribe(
  homeboxsp => {
    this.homeboxsp = homeboxsp.sensors;
       }
);

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222722

You need another ngFor to loop sensor objects

<tr *ngFor="let item of homeboxsp">
        <td>{{item.serial_number}}</td>
        <ng-container *ngFor= "let sensor of item.sensors">
        <ul>
          <li>{{sensor.sensors_id}}</li>
        </ul>
        <td>{{item.active}}</td>
</tr>

Upvotes: 3

Related Questions