[英]Angular Data not rendering in the html
I am trying to implement an api call in stackblitz and display the records in the html page. 我正在尝试在stackblitz中实现api调用,并在html页面中显示记录。 For some reason I am getting no errors but neither is any data getting rendered on the screen
由于某种原因,我没有收到任何错误,但屏幕上也没有呈现任何数据
I am not sure what the problem is 我不确定是什么问题
Here is the stackblitz 这是堆叠闪电战
https://stackblitz.com/edit/angular-cjqmyg?file=src/app/app.component.html https://stackblitz.com/edit/angular-cjqmyg?file=src/app/app.component.html
app.component.html app.component.html
<app-list-wrapper></app-list-wrapper>
ListWrapperComponent ListWrapperComponent
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 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>
Service code 服务编号
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/');
}
}
You does not subscribe
your Observable
,so you never call your service. 您没有
subscribe
Observable
,因此您永远不会致电服务。
And the url is https://api.coingecko.com/api/v3/coins/
,so you could not use this.webApplication + url
in your service httpGet
. 网址是
https://api.coingecko.com/api/v3/coins/
,因此您不能在服务httpGet
使用this.webApplication + url
。
You cold check the json data from this json formatter by inputing the url 您通过输入网址来从此json格式化程序中冷检查json数据
Refer to below changes: 请参考以下更改:
1.SmartTrCommonService.ts 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 2.ListWrapperService.ts
getBitCoins() : Observable<IListWrapper[]>{
return this.smartTrCommonService.httpGetTest('/api.coingecko.com/api/v3/coins/');
}
3.list-wrapper.Component.ts 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 has no address and image is an array which you need to change) 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>
Update: 更新:
In list-wrapper.Cpmponent.ts, this.bitcoins$ = this.listWrapperService.getBitCoins()
could do the subscribtion automatically, so the main problem lies in that the url is not correct. 在list-wrapper.Cpmponent.ts中,
this.bitcoins$ = this.listWrapperService.getBitCoins()
可以自动执行订阅,因此主要问题在于网址不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.