在控制台中接收此错误:
无法找到“对象”类型的不同支持对象“对象对象”。NgFor只支持绑定到诸如数组之类的Iterable。
services.ts
private url = "https://api.iextrading.com/1.0/stock/market/batch?symbols=SNAP,fb,AIG%2b,GOOG,AAPL,AMZN&types=quote";
constructor(private http: HttpClient) {}
getCID(): Observable<any> {
return this.http.get<any>(this.url);
}component.ts
ngOnInit() {
this.svc.getCID().subscribe(data => {
this.cidData = data;
console.log(data);
});component.html
<div *ngIf="cidData">
<ul>
<li *ngFor="let x of cidData">
<a routerLink="/companydetails/{{x.symbol}}">{{x.symbol}}</a>API调用
{"GOOG":{"quote":{"symbol":"GOOG","companyName":"Alphabet Inc."}}, "SNAP":{etc...}我认为问题是cidData不是数组,但我不确定如何将它作为数组返回
发布于 2018-12-22 06:32:09
如果您使用的是角版本6,那么您可以使用内置管道键值来迭代对象。
<li *ngFor="let x of cidData | keyvalue">
<a routerLink="/companydetails/{{x.value.quote.symbol}}">{{x.value.quote.symbol}}</a>发布于 2018-12-22 07:04:49
若要通过Object.values(obj)将对象转换为数组,您可以看到here
// object with your example here
ngOnInit() {
this.svc.getCID().subscribe(data => {
this.cidData = data;
let array =
Object.values(this.cidData.GOOD)
// here you have an array
console.log(array);
});
https://stackoverflow.com/questions/53893518
复制相似问题