【发布时间】:2019-12-31 14:42:03
【问题描述】:
我想在有角项目的卡片中显示,例如行的计数。要从 mysql 获取数据,我正在使用服务。
这里是服务数据:
private _postsURL = "http://localhost:3003/all";
constructor(private http: Http) {
}
getcount(): Observable<ICustomer[]> {
return this.http
.get(this._postsURL)
.map((response: Response) => {
return <ICustomer[]>response.json();
})
.catch(this.handleError);
}
组件.ts
getcount(): void {
this.apiSerivce.getcount()
.subscribe(
resultArray => this._count = [resultArray[0]],
error => console.log("Error :: " + error)
)
}
ngOnInit(): void {
this.getcount();
component.html
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-12" *ngFor="let post of _count">
<h2 class="m-b-0"><i class="mdi mdi-buffer text-
warning"></i></h2>
<h3 class="">{{post}}</h3>
<h6 class="card-subtitle">Total Earnings</h6>
</div>
<div class="col-12">
<ngb-progressbar [showValue]="false"
type="warning" [value]="50"></ngb-progressbar>
我只想使用从 api 获取的值来填充卡片。
【问题讨论】:
-
首先要问一个问题,你为什么用
Http而不是HttpClient? -
你应该包括一个当前收到的数据的例子,即当你控制台记录 resultArray 响应时有什么?