angular - 角度数据未在 html 中呈现
问题描述
我正在尝试在 stackblitz 中实现 api 调用并在 html 页面中显示记录。出于某种原因,我没有收到任何错误,但屏幕上也没有呈现任何数据
我不确定问题是什么
这是堆栈闪电战
https://stackblitz.com/edit/angular-cjqmyg?file=src/app/app.component.html
app.component.html
<app-list-wrapper></app-list-wrapper>
ListWrapper 组件
import {ListWrapperService} from '../service/list-wrapper.service';
import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Observable, of, EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Component({
selector: 'app-list-wrapper',
templateUrl: './list-wrapper.component.html',
styleUrls: ['./list-wrapper.component.css']
})
export class ListWrapperComponent implements OnInit {
bitcoins$: Observable<IListWrapper[]>;
errorMessage: any;
constructor(private listWrapperService : ListWrapperService) { }
ngOnInit() {
this.bitcoins$ = this.listWrapperService.getBitCoins()
.pipe(
catchError(err => {
this.errorMessage = err;
return EMPTY;
})
)
}
}
html
<div class="container">
<div *ngIf="bitcoins$ | async as bitcoins">
<table class="table table-striped">
<thead>
<tr>
<th>symbol</th>
<th>name</th>
<th>Contact Title</th>
<th>block_time_in_minutes</th>
<th>image</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let bitcoin of bitcoins">
<td>{{ bitcoin.symbol }}</td>
<td>{{ bitcoin.name }}</td>
<td>{{ bitcoin.block_time_in_minutes }}</td>
<td>{{ bitcoin.address }}</td>
<td>{{ bitcoin.image }}</td>
</tr>
</tbody>
</table>
</div>
</div>
服务代码
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { SmartTrCommonService } from '../shared/smarttr.common.service';
@Injectable({
providedIn: 'root'
})
export class ListWrapperService {
constructor(private smartTrCommonService: SmartTrCommonService) { }
getBitCoins() : Observable<IListWrapper[]>{
return this.smartTrCommonService.httpGet('/api.coingecko.com/api/v3/coins/');
}
}
解决方案
你没有subscribe
你的Observable
,所以你从不打电话给你的服务。
而且网址是https://api.coingecko.com/api/v3/coins/
,所以你不能this.webApplication + url
在你的服务中使用httpGet
。
您通过输入 url从这个json 格式化程序冷检查 json 数据
请参阅以下更改:
1.SmartTrCommonService.ts
httpGetTest(url: string): Observable<any> {
return this.httpClient.get('https:' + '//' + url)
.pipe(map((response: Response) => {
return response;
}), catchError(error => {
this.onError(error);
return Promise.reject(error);
}));
}
2.ListWrapperService.ts
getBitCoins() : Observable<IListWrapper[]>{
return this.smartTrCommonService.httpGetTest('/api.coingecko.com/api/v3/coins/');
}
3.list-wrapper.Component.ts
export class ListWrapperComponent implements OnInit {
bitcoins:IListWrapper[];
errorMessage: any;
constructor(private listWrapperService : ListWrapperService) { }
ngOnInit() {
this.listWrapperService.getBitCoins()
.subscribe(bitcoins => this.bitcoins = bitcoins,error => this.errorMessage = error)
}
}
4.list-wrapper.Component.html(json没有地址,图片是数组,需要修改)
<div class="container">
<div *ngIf="bitcoins">
<table class="table table-striped">
<thead>
<tr>
<th>symbol</th>
<th>name</th>
<th>Contact Title</th>
<th>block_time_in_minutes</th>
<th>image</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let bitcoin of bitcoins">
<td>{{ bitcoin.symbol }}</td>
<td>{{ bitcoin.name }}</td>
<td>{{ bitcoin.address }}</td>
<td>{{ bitcoin.block_time_in_minutes }}</td>
<td>{{ bitcoin.image }}</td>
</tr>
</tbody>
</table>
</div>
</div>
更新:
在list-wrapper.Cpmponent.ts中,this.bitcoins$ = this.listWrapperService.getBitCoins()
可以自动进行订阅,所以主要问题在于url不正确。
推荐阅读
- kotlin - Kotlin 获取声明的成员属性值
- java - 我创建了带有标题和持续时间的歌曲类,以及带有 ArrayList 的专辑类,我正在尝试打印列表但在哈希值中给出输出
- reactjs - 如何修改reducer中的现有数据?
- c++ - 在内核级别安全隐藏文件
- recursion - 涂漆 N 个方块和 M 个漆块的独特组合的数量,每个漆块长度为 K 个方块
- android - ViewBinding vs Kotlin Android Extensions 与合成视图
- python - 我在 Python 中编写了一个代码来从 5 个元素的数组中找到 4 个元素的最大值和最小值
- python - Keras自定义损失函数与马氏距离损失如何
- dataframe - 如何将 DBI ODBC SQL 查询下载到文件
- c# - 实现没有饥饿的while循环以在wpf中重绘图像