Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

angular - Angular4 Error trying to diff '[object Object]'

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 do is iterate over this data from https://www.surbtc.com/api/v2/markets/btc-clp/ticker:

{"ticker":{"last_price":["1771455.0","CLP"],"min_ask":["1771432.0","CLP"],"max_bid":["1660003.0","CLP"],"volume":["178.37375119","BTC"],"price_variation_24h":"-0.107","price_variation_7d":"-0.115"}}

I want to display it in the html like this:

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

this is the service.ts:

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {Observable} from "rxjs";
import 'rxjs/Rx';
import 'rxjs/add/operator/catch';
import { SurbtcMarket } from './surbtcmarket'


@Injectable()
export class SurbtcService {

  constructor(private http: Http, private surBtcMarket : SurbtcMarket) { }

  public getPricess() :Observable<SurbtcMarket> {
    return this.http.get('https://www.surbtc.com/api/v2/markets/btc-clp/ticker')
    .map((response: Response) => response.json());
  }

}

Interface surbtcmarket.ts

export class SurbtcMarket {
public ticker: SurbtcMarketView[];
}

export class SurbtcMarketView {
  public last_price : number;
  public min_ask : number;
  public max_bid : number;
  public volume : number;
  public price_variation_24h : number;
  public price_variation_7d : number;
}

component.ts

import { Http, Response, Headers } from '@angular/http';
import { SurbtcService } from '../../surbtc/surbtc.service';
import {Observable} from "rxjs";

@Component({
  selector: 'app-comprarltc',
  templateUrl: './comprarltc.component.html',
  styleUrls: ['./comprarltc.component.scss']
})
export class ComprarltcComponent implements OnInit {

  private prices = [];

  constructor(private surbtcService: SurbtcService) {
    this.surbtcService = surbtcService;
  }

ngOnInit(){
  this.surbtcService.getPricess()
  .subscribe(
    data => this.prices = data.ticker
  );
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...