[英]Fetching Array values from json api in angular 6
API API
{"Data":[{"SYMBOL":"XBTUSD","SUPPLY":0,"FULLNAME":"XBTUSD","NAME":"XBTUSD","ID":-1,"VOLUME24HOURTO":1666015703.2591257},{"SYMBOL":"BTC","SUPPLY":17294062,"FULLNAME":"Bitcoin (BTC)","NAME":"Bitcoin","ID":"1182","VOLUME24HOURTO":487020997.4281679},{"SYMBOL":"ETH","SUPPLY":102248936.0928,"FULLNAME":"Ethereum (ETH)","NAME":"Ethereum","ID":"7605","VOLUME24HOURTO":189913859.14174834}],
"Type":100,
"Response":"Success",
"VolSymbol":"USD",
"Message":"All ok"}
Data.service.ts Data.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class DataService {
Data:any;
constructor(private _http: HttpClient) {}
getTop() {
return this._http.get("https://min-api.cryptocompare.com/data/top/volumes?tsym=USD&limit=4")
.pipe(map(result => this.Data = result));
}
}
prices.component.ts prices.component.ts
export class PricesComponent implements OnInit {
cryptos: Array<Object>;
constructor(private dataService: DataService)
{
this.dataService.getTop().subscribe(res => {
this.cryptos = res.Data;
console.log(res);
});
}
ngOnInit() {
}
}
I want to fetch All Data Values listed in that Array. 我想获取该数组中列出的所有数据值 。 I am getting error in prices.component.ts as it is unable to read the value Data.
我在prices.component.ts中遇到错误,因为它无法读取值Data。
I am also not sure how to loop the values in Data and print the corresponding objects in html file. 我也不确定如何循环数据中的值并在html文件中打印相应的对象。 I want Something similar in html file:
我想要html文件中类似的内容:
SYMBOL:"BTC" 符号: “BTC”
SUPPLY:17294062 供应:17294062
FULLNAME:"Bitcoin 全名:“比特币
VOLUME24HOURTO":487020997.4281679 VOLUME24HOURTO“:487020997.4281679
SYMBOL:"ETH" 符号: “ETH”
SUPPLY:102248936.0928 供应:102248936.0928
FULLNAME:"Ethereum 全名:“复仇
VOLUME24HOURTO:189913859.14174834 VOLUME24HOURTO:189913859.14174834
Your issue with .Data may be solved with res['Data']
. 您的.Data问题可以通过
res['Data']
。 Also, for looping in html try: 另外,要在html中循环播放,请尝试:
in ts: 在ts中:
this.map = {}
for (key in res['Data']) {
let el = {}
el['key'] = key
el['val'] = res['Data'][key]
this.map.push(el)
}
in html: 在html中:
<div *ngFor="let el in map">
{{el.key}}: {{el.value}}
</div>
1) DataService 1)数据服务
ng g s Data
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class DataService {
Data:any;
constructor(private _http: HttpClient) {}
getTop() {
return this._http.get<any>("https://min-api.cryptocompare.com/data/top/volumes?tsym=USD&limit=4");
}
}
2) AppModule.ts 2)AppModule.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { PricesComponent } from './prices/prices.component';
const appRoutes: Routes = [
{ path: '', component: PricesComponent }
];
@NgModule({
declarations: [
AppComponent,
PricesComponent
],
imports: [
BrowserModule,
HttpClientModule,
RouterModule.forRoot(
appRoutes,
{ enableTracing: true } // <-- debugging purposes only
)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
3) PricesComponent.ts 3)PriceComponent.ts
ng g c Prices
import { Component, OnInit } from '@angular/core';
import {DataService} from '../data.service'
@Component({
selector: 'app-prices',
templateUrl: './prices.component.html',
styleUrls: ['./prices.component.css']
})
export class PricesComponent implements OnInit {
cryptos:any;
constructor(private dataService: DataService)
{
this.dataService.getTop().subscribe(res => {
this.cryptos = res['Data'];
console.log(this.cryptos);
});
}
ngOnInit() {
}
}
4) Pricecomponent.html 4)Pricecomponent.html
<div *ngFor="let el of cryptos">
FULLNAME {{el.FULLNAME}}--
SYMBOL {{el.SYMBOL}}--
SUPPLY {{el.SUPPLY}}--
NAME {{el.NAME}}--
VOLUME24HOURTO {{el.VOLUME24HOURTO}}
</div>
5) AppComponent.html 5)AppComponent.html
<router-outlet></router-outlet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.