![](/img/trans.png)
[英]How can I show & hide simple CSS spinner while fetching data from database with my service - Angular 2>
[英]Fetching data from database into angular datatable
我有一些數據存儲在數據庫中,我想將其提取並放置在我的角度數據表中,但是它無法解決問題,我也找不到原因。 雖然,我在構造函數中創建的實例顯示在我的角度表中,但是我希望能夠將數據庫中存儲的數據請求到我的角度數據表中。 謝謝!
customer.ts文件
import {Component, OnInit} from '@angular/core';
import {HttpService} from "./Client_Service";
import {Response} from '@angular/http';
@Component({
selector: 'client',
templateUrl:'client_table.html',
providers:[HttpService]
})
export class ClientComponent{
welcome: string;
clients: [{
Name: string,
Email: string,
Company: string
}];
constructor(){
this.welcome = "Customer Listing"
this.clients = [{
Name: "John Jan",
Email:"example.com",
Company: "Metabo"
}
]
};
customers:Customer[];
constructor(private httpService: HttpService){}
ngOnInit(){
this.httpService.getCustomer()
.subscribe(
(data: Response ) => console.log(data.json())
);
}
}
//我的角度數據表
<h1>{{welcome}}</h1>
<table class="table">
<tr>
<th>#</th>
<th> Name</th>
<th>Email</th>
<th>Company</th>
</tr>
<tr *ngFor="let client of clients; let i = index">
<td>{{i + 1}}</td>
<td>{{client.Name}}</td>
<td>{{client.Email}}</td>
<td>{{client.Company}}</td>
</tr>
</table>
// Http服務
import {Injectable} from '@angular/core';
import {Response, Http} from '@angular/http';
import {DataListModule} from 'primeng/primeng';
@Injectable()
export class HttpService{
constructor(private http:Http){
}
getCustomer(){
//using get request
return this.http.get('http://localhost:9000/api/crm_api/v1/customers')
.map((response: Response) => response.json())
}
}
如果
this.httpService.getCustomer()
.subscribe(
(data: Response ) => console.log(data.json())
);
正在工作,您所要做的就是:
this.httpService.getCustomer()
.subscribe(
(data: Response ) => {
console.log(data.json())
this.clients = data;
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.