简体   繁体   中英

Displaying JSON Data Using Angular 7

I am attempting to display this JSON data from the server:

{"buySell":
[
  {"date":"2015-03-02","close":120.351,"buySell":"Sell"},
  {"date":"2019-01-02","close":157.2455,"buySell":"Buy"}
],

"firstReturn":0.62987323647548421,
"sell":{"date":"2018-10-08","close":222.0396},
"buy":{"date":"2018-05-02","close":173.9216}
}

This is how I format it in HTML:

<mat-list *ngIf="buySellData$ | async as stock else noData">
   <mat-list-item *ngFor="let item of stock ">
       {{item.listOfBuySell | json}} 
   </mat-list-item>
</mat-list>

This is the stock format

stock: Cagr[];

and carg.ts is:

import { Istock } from './istock'
import { Dateclose } from './dateclose'

export class Cagr {
    listOfBuySell: Istock;
    close: number;
    sell: Dateclose;
    buy : Dateclose;
 }

istock.ts is:

 export class Istock {
    date: Date;
    close: number;
    buysell: string;
}

and finally dateclose.ts

export class Dateclose {
    date: Date;
    close: number;
}

This is the error I get in displaying:

Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

You are trying to iterate over an object but not array . This causes such error.

You should check what exactly you have as a result in buySellData$ | async buySellData$ | async .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM