gpt4 book ai didi

angular - 尝试 *ngFor Json 对象时出错

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:25 25 4
gpt4 key购买 nike

我正在尝试创建一个从端点返回的数据列表,我得到了 10 位数据,我想使用 *ngFor 来显示它们。我已经在正确的时间正确输入了数据,但它说的是

ERROR Error: "Cannot find a differ supporting object '[object Promise]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."

但是据我所知,您可以在最近版本的 Angular 中使用 *ngFor 中的 json。

JSON 返回:https://pastebin.com/TTn0EqSS

app.component.html

<div [class.app-dark-theme]="true">
<mat-sidenav-container fullscreen class="sidenav-container">
<mat-toolbar class="toolbar">
Coin Market Cap 3rd party api app
</mat-toolbar>

<mat-card>
<mat-card-header>
<mat-card-title>CryptoCurrency Market Overview</mat-card-title>
<mat-card-subtitle>Top 15 current currencies.</mat-card-subtitle>
</mat-card-header>

<mat-card-content class="currency-listings">
<div *ngIf="finishedLoading">
<mat-grid-list cols="1" rowHeight="2:1">
<mat-grid-tile *ngFor="let currency of currenciesJson; let i = index" (click)="selectCurrency(i)">
{{currency.data[i].name}}
</mat-grid-tile>
</mat-grid-list>

test
test
test
</div>
</mat-card-content>
</mat-card>

<!-- (click)="showInfo(true)" -->

<mat-card *ngIf="displayInfo">
test
</mat-card>
</mat-sidenav-container>
</div>

coin-market-cap.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class CoinMarketCapService
{
key = "REDACTED";
apiUrl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/';

constructor(public http: HttpClient) { }

getCurrencies(totalCurrencies: number)
{
let promise = new Promise((resolve, reject) => {
let url = this.apiUrl + "listings/latest?limit=" + totalCurrencies + "&CMC_PRO_API_KEY=" + this.key;
this.http.get(url)
.toPromise()
.then(
res => {
console.log(res);
resolve();
});
})
return promise;
}

getCurrency(currencyId: number)
{
console.log("in getcurrency");
let url = this.apiUrl + "info?id=" + currencyId + "&CMC_PRO_API_KEY=" + this.key;
console.log(url);
return this.http.get(url);
}
}

应用程序组件.ts

import { Component } from '@angular/core';
import { CoinMarketCapService } from '../services/coin-market-cap.service';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent
{
currenciesJson = {};
displayInfo = false;
finishedLoading = false;

constructor(private CoinMarketCapService: CoinMarketCapService) {}

ngOnInit()
{
console.log(this.currenciesJson);
this.currenciesJson = this.CoinMarketCapService.getCurrencies(10)
.then(res =>
this.finishedLoading = true
)
console.log(this.currenciesJson);
console.log("exiting ngoninit");
}

selectCurrency(currencyId: number)
{
console.log(currencyId);
let currencyObject = this.CoinMarketCapService.getCurrency(currencyId);
}

showInfo ( showInfo: boolean )
{
this.displayInfo = showInfo;
}
}

最佳答案

在 Angular 6.1.something(最近发布)中,有一个用于迭代 JSON 对象的管道。

它调用 KeyValuePipe - 文档在这里 - https://angular.io/api/common/KeyValuePipe

<div *ngFor="let item of object | keyvalue">
{{item.key}}:{{item.value}}
</div>

这太新了,你必须使用最新版本的 Angular 6。如果你已经在使用 Angular 6,那么只需运行:

ng update @angular/core
ng update @angular/cli

获取最新信息。

这是一篇关于它的文章 - http://www.talkingdotnet.com/angular-6-1-introduces-new-keyvalue-pipe/

关于angular - 尝试 *ngFor Json 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51732831/

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