Angular4 Error trying to diff '[object Object]'

后端 未结 3 551
遇见更好的自我
遇见更好的自我 2021-01-27 04:11

I\'m trying to display some info in the dom but I get this error:

Error: Error trying to diff \'[object Object]\'

What Im trying to

相关标签:
3条回答
  • 2021-01-27 04:47
    public getPricess() :Observable<SurbtcMarket> { return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker') .map((response: Response) => response.json() as SurbtcMarket); }
    

    This might be due to the fact as it's not mapped to a type. And I assume this only retun one object otherwise you have to return an array.

    public getPricess() :Observable<SurbtcMarket[]> { return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker') .map((response: Response) => response.json() as SurbtcMarket[]); }
    
    0 讨论(0)
  • 2021-01-27 05:04

    If you want to have price as an array, then you have push the object in prices array.

    ngOnInit(){
      this.surbtcService.getPricess()
      .subscribe(
        data => this.prices.push(data.ticker);
      );
    }
    

    OR, you can just directly access the object properties by assigning data.ticker to prices.

    private prices = new SurbtcMarketView();
    
    constructor(private surbtcService: SurbtcService) {
    
    }
    ngOnInit(){
       this.surbtcService.getPricess()
            .subscribe(data =>{
                this.prices = data.ticker;
            });
    }
    

    HTML:

    <div *ngFor="let item of prices.min_ask">
      {{item}}
    </div>
    

    UPDATE:

    See the Plnkr demo, I have resolved the issue that was causing error in parsing the json response.

    0 讨论(0)
  • 2021-01-27 05:09

    Else you can also use this ,

    prices : SurbtcService[] = [];
    

    And then you can push the user object and make as a array,

    this.prices.push(<<the data you want to push>>);
    
    0 讨论(0)
提交回复
热议问题