我试图在dom中显示一些信息,但是我得到了以下错误:
错误:试图区分“对象”的错误
我想要做的是迭代来自https://www.surbtc.com/api/v2/markets/btc-clp/ticker的这些数据
{“last_price”:{“1771455.0”、“中电”、"min_ask":"1771432.0“、”中电“、"max_bid":"1660003.0”、“中电”、“卷”:“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());
}
}接口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
);
}
}发布于 2017-06-15 20:26:24
如果希望将price作为数组,则在prices数组中有push对象。
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 演示,我已经解决了在解析json响应时导致错误的问题。
发布于 2019-06-04 07:24:59
否则你也可以用这个,
prices : SurbtcService[] = [];然后您可以将用户对象推送到数组中,
this.prices.push(<<the data you want to push>>);https://stackoverflow.com/questions/44574249
复制相似问题