2017-06-14 73 views
0

我試着迭代數組,但是dom改爲顯示[object Object]。在其他線程中,有人建議使用Stringify,然後顯示信息,但我無法迭代字符串。謝謝你的幫助。Angular 4 [對象對象]

這裏是我的代碼:

HTML

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

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 { MarketViewModel } from '../comprarmonedas/datosmoneda' 


@Injectable() 
export class BittrexService { 

    constructor(private http: Http, private marketModel : MarketViewModel) { } 

    public getPrices() :Observable<MarketViewModel> { 
    return this.http.get('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-zec') 
    .map((response: Response) => response.json()); 
    } 

} 


interface 

export class MarketViewModel { 
    public success : boolean; 
    public message : string; 
    public result : MarketListObject[]; 
} 

export class MarketListObject { 
    public MarketName : string; 
    public High : number; 
    public Low : number; 
    public Volume : number; 
    public Last : number; 
    public BaseVolume : number; 
    public TimeStamp : number; 
    public Bid : number; 
    public Ask : number; 
    public OpenBuyOrders : number; 
    public OpenSellOrders : number; 
    public PrevDay : number; 
    public Created : number; 

} 

component.ts

import { Component, OnInit } from '@angular/core'; 
import { Http, Response, Headers } from '@angular/http'; 
import { BittrexService } from '../../bittrex/bittrex.service'; 
import {Observable} from "rxjs"; 

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

    private prices = []; 

    constructor(private bittrexService: BittrexService) { 
    this.bittrexService = bittrexService; 
    } 

ngOnInit(){ 
    this.bittrexService.getPrices() 
    .subscribe(
    data => this.prices = data.result 
); 
} 
} 
+0

代替印刷每個價格,你將列表合併成一個字符串 –

+0

@nickzoum我是一個新手。你是什​​麼意思,我該如何解決它?感謝您的回覆 –

+0

這意味着您正在嘗試將很多對象合併到一個字符串中。下面的答案顯示瞭如何解決它。 –

回答

3

替換此:

<div *ngFor="let price of prices"> 
    High : {{price.High}} , Low : {{price.Low}} 
</div> 

您嘗試打印obejcts prices的陣列, 應該priceprices

High : {{price.High}} , Low : {{price.Low}} 

這樣你就可以訪問任何給定的值:

{ 
     "MarketName": "BTC-ZEC", 
     "High": 0.16290000, 
     "Low": 0.13087156, 
     "Volume": 12760.98721068, 
     "Last": 0.15650003, 
     "BaseVolume": 1908.20341779, 
     "TimeStamp": "2017-06-14T19:15:25.57", 
     "Bid": 0.15650003, 
     "Ask": 0.15786551, 
     "OpenBuyOrders": 1130, 
     "OpenSellOrders": 1257, 
     "PrevDay": 0.13380000, 
     "Created": "2016-10-28T17:13:10.833" 
    } 
+0

請問你也接受我的答案嗎? –

+0

我得到同樣的錯誤[對象對象] –

+0

@ClaudioRamírezGuichard,你能告訴我價格的價值嗎? –