2017-01-02 3 views
1

Я хочу получить данные из своей базы данных и отобразить их в таблице данных. Когда я запускаю свое приложение, я вижу на вкладке сети ответ, который был получен, но я не показываю его в таблице. Вот мой код;Извлечь данные из базы данных в таблицу с угловыми 2

// Client.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 implements OnInit{ 
    welcome: string; 

    Clients: [{ 
    first_name:string, 
    last_name: string, 
    email: string, 
    company_name: string, 
    mobile: string, 
    city: string, 
    website:string 
    }]; 
    constructor(private httpService: HttpService){ 
    this.Clients = { 
     first_name:"", 
     last_name: '', 
     email: '', 
     mobile: '', 
     company_name: ' ', 
     city: '', 
     website:'.' 
    }[]; 
    } 

    ngOnInit(){ 
    this.httpService.getCustomer() 
    .subscribe(
     (data: any) =>{ 
     console.log(data.json()); 
     this.Clients = data 
     }) 
    } 
} 

// Http сервис

import {ClientComponent} from './Clients' 
import {Injectable} from '@angular/core'; 
import {Response, Http} from '@angular/http'; 
import 'rxjs/Rx'; 




@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()); 
    } 
} 

// Html стол

<h1>{{welcome}}</h1> 
<table class="table"> 
    <tr> 
    <th>#</th> 
    <th>FirstName</th> 
    <th>LastName</th> 
    <th>Email</th> 
    <th>Company</th> 
    <th>Mobile</th> 
    <th>City</th> 
    <th>Website</th> 
    </tr> 
    <tr *ngFor="let client of Clients; let i = index"> 
    <td>{{i + 1}}</td> 
    <td>{{client.first_name}}</td> 
    <td>{{client.last_name}}</td> 
    <td>{{client.email}}</td> 
    <td>{{client.mobile}}</td> 
    <td>{{client.company_name}}</td> 
    <td>{{client.city}}</td> 
    <td>{{client.website}}</td> 
    </tr> 
</table> 

// трассировки стека

ClientComponent/<@http://localhost:4200/main.bundle.js:33304:25 
SafeSubscriber</[email protected]://localhost:4200/main.bundle.js:632:13 
SafeSubscriber</[email protected]://localhost:4200/main.bundle.js:581:17 
Subscriber</[email protected]://localhost:4200/main.bundle.js:534:9 
Subscriber</[email protected]://localhost:4200/main.bundle.js:498:13 
MapSubscriber</[email protected]://localhost:4200/main.bundle.js:10596:9 
Subscriber</[email protected]://localhost:4200/main.bundle.js:498:13 
XHRConnection/this.response</[email protected]://localhost:4200/main.bundle.js:46041:21 
Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:101091:21 
NgZone</NgZone.prototype.forkInnerZoneWithAngularBehavior/this.inner<[email protected]://localhost:4200/main.bundle.js:30795:28 
Zone$1</ZoneDelegate</[email protected]://localhost:4200/main.bundle.js:101090:21 
Zone$1</Zone</[email protected]://localhost:4200/main.bundle.js:100980:28 
ZoneTask/[email protected]://localhost:4200/main.bundle.js:101161:28 

Api response

+0

вы записываете 'console.log (data.json());' но присваиваете 'this.Clients = data', не должны ли они снова быть версией json? – echonax

+0

@echonax, я не очень понятен с вашим заявлением, но если я прав, я уже установил версию Json в строку this.Clients = данные и все те же результаты –

+0

Я говорю, что пытаюсь установить его в 'this. Клиенты = data.json() ' – echonax

ответ

0

Вы должны объявить Clients как ниже внутри компонента:

Clients: { 
    first_name:string, 
    last_name: string, 
    email: string, 
    company_name: string, 
    mobile: string, 
    city: string, 
    website:string 
}[]; 
+0

@ ranakrnal9, ничего не произошло! –

+0

делитесь своим ответом, который вы получаете от API. – ranakrunal9

+0

Я только что разместил выстрел из Api respone. Я попытался скопировать его, но не работал, поэтому я отправил снимок экрана. –

0

Вы должны импортировать rxjs в свой проект.

+0

Те же результаты. Нет данных! –

Смежные вопросы