0

I am trying to use ngfor on a nested json output. The first loop is working fine but the inner loop is not working. My code is

Ts file

import { Component, OnInit } from '@angular/core';
import { TiffinService } from '../tiffin.service';
import { IVen } from '../vendor';

@Component({
  selector: 'app-tabletest',
  templateUrl: './tabletest.component.html',
  styleUrls: ['./tabletest.component.css']
})
export class TabletestComponent implements OnInit {
  vend: IVen[];

  constructor(private apiService: TiffinService) { }

  ngOnInit(){
    this.apiService.getVendor('1')
          .subscribe(
          resultArray => {
          this.vend = resultArray;
        });
  }
}

output of the service is

[{"Cat":[{"Catname":"Daily","Catprice":15.99},{"Catname":"Weekly","Catprice":99.99}],"Vaddress1":"vad1","Vaddress2":"vad2","Vcell":"123456789","Vcity":"milton","Vemail":"[email protected]","Vid":1,"Vname":"vendor1","Vpcode":"l9t0p2","Vphone":"123456789"},{"Cat":[{"Catname":"Daily","Catprice":15.99},{"Catname":"Weekly","Catprice":110.99}],"Vaddress1":"vad1","Vaddress2":"vad2","Vcell":"123","Vcity":"milton","Vemail":"[email protected]","Vid":2,"Vname":"vendor2","Vpcode":"l9","Vphone":"123"}]

html file is

<div *ngFor="let item of vend">
   <table>

      <ng-container>
        <tr>
          <td colspan="2">{{item.Vname}}</td>
        </tr>
        <tr *ngFor="let value of vend.Cat">
          <td>{{value.Catname}}</td>
        </tr>
      </ng-container>
   </table>
<div>

1 Answer 1

1

You need to access item.Cat instead of vend which is retrieved from the parent loop

<div *ngFor="let item of vend">
   <table>    
      <ng-container>
        <tr>
          <td colspan="2">{{item.Vname}}</td>
        </tr>
        <tr *ngFor="let value of item.Cat">
          <td>{{value.Catname}}</td>
        </tr>
      </ng-container>
   </table>
<div>

STACKBLITZ DEMO

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

Comments

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.