gpt4 book ai didi

Angular4 尝试比较 '[object Object]' 时出错

转载 作者:可可西里 更新时间:2023-11-01 16:32:13 26 4
gpt4 key购买 nike

我试图在 dom 中显示一些信息,但出现此错误:

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

我想做的是迭代来自 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"}}

我想像这样在 html 中显示它:

<div *ngFor="let price of prices">
{{price.min_ask}}
</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 { 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;
}

组件.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
);
}
}

最佳答案

如果您想将 price 作为一个数组,那么您可以push prices 数组中的对象。

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

或者,您可以通过将 data.ticker 分配给 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>

更新:

查看 Plnkr demo ,我已经解决了导致解析 json 响应错误的问题。

关于Angular4 尝试比较 '[object Object]' 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com