2016-12-28 78 views
1

我有一些数据存储在我的数据库中,我想要获取并放置在我的角度数据表中,但它不工作,我找不到原因。虽然,我在构造函数中创建的实例出现在我的角度表中,但我希望能够将我数据库中存储的数据请求到角度数据表中。谢谢!从数据库提取数据到角度数据表

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()) 



    } 





} 

回答

3

如果

this.httpService.getCustomer() 
     .subscribe(
     (data: Response) => console.log(data.json()) 
    ); 

工作,所有你需要做的是:

this.httpService.getCustomer() 
     .subscribe(
     (data: Response) => { 
      console.log(data.json()) 
      this.clients = data; 
     } 
    ); 
+0

我不断收到此错误:Type“响应是不能分配给输入” [{FIRST_NAME:串; last_name:string ... – Switz

+0

删除'Response'给它一个类型接口或者像下面这样做:'(data:any)=> {'而不是'(data:Response)=> {' – echonax

+0

非常感谢你..我现在看到控制台中的响应。将检查如何在桌上展示他们。非常感谢 – Switz