2017-10-12 66 views
0

我剛剛開始研究節點和角度,我無法將JSON數據傳遞給我的角度組件,並在點擊屏幕上將其呈現在屏幕上一個按鈕。感謝您的幫助。如何將JSON數據從節點發送到角4

這是我要傳遞的JSON數據

{ 
Customer_ID: 1507843123970, 
Bank_Account_Number: 7885236985412589, 
Bank_Name: "Some Bank", 
Bank_Address1: "Some Address", 
Bank_Address2: null, 
Account_Holder_Name: "Some Name", 
Account_Type: "Savings", 
Transaction_Limit: 5000 
} 

我的服務文件看起來像這樣

@Injectable() 
export class ServerService { 
constructor(private http: Http) {} 

getServer() { 
    return this.http.get('http://localhost:3000/addFunds') 
    .map(
     (response: Response) => { 
     const data = response.json(); 
     return data; 
     } 
    ); 
    } 
} 

而且打字稿文件看起來像這樣

export class AddFundComponent implements OnInit { 
@ViewChild('addFundForm') addFundForm: NgForm; 
showHidden = false; 
showBankDetail = false; 
showSubmit = true; 

servers: Observable<any>; 

constructor(private serverService: ServerService) { } 

ngOnInit() { 
    this.servers = this.serverService.getServer(); 
} 
onGet() { 
    this.serverService.getServer().subscribe(
    (data) => { 
     // const data = response.json(); 
     console.log(data); 
    }, 
    (error) => console.log(error) 
); 
} 
} 
+0

你爲什麼要在內部映射器中創建const?你可以返回response.json() –

+0

好..我回來的數據分配給response.json() – chirag

回答

2

的NodeJS + expressjs

const express = require('express'), 
     config = require('./config'), 
     app = express(); 

app.get('/addFunds', (req, res) => { 
     res.json({ 
      Customer_ID: 1507843123970, 
      Bank_Account_Number: 7885236985412589, 
      Bank_Name: "Some Bank", 
      Bank_Address1: "Some Address", 
      Bank_Address2: null, 
      Account_Holder_Name: "Some Name", 
      Account_Type: "Savings", 
      Transaction_Limit: 5000 
     }); 
    }); 

app.listen(config.port, function(){ 
    console.log('http://127.0.0.1:' + config.port); 
}); 

module.exports = app; 

http.service.ts

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response, RequestOptions } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/observable/throw'; 

@Injectable() 
export class HttpService { 
    private actionUrl: string; 
    private headers: Headers; 
    private options: RequestOptions; 

    constructor(public _http: Http) { 
    this.actionUrl = 'example.com'; 
    this.headers = new Headers(); 
    this.headers.append('Content-Type', 'application/json'); 
    this.headers.append('Accept', 'application/json'); 
    this.headers.append('Access-Control-Allow-Headers', 'Content-Type, X-XSRF-TOKEN'); 
    this.headers.append('Access-Control-Allow-Origin', '*'); 
    this.options = new RequestOptions({ headers: this.headers }); 
    } 

    get(request: string): Observable<any> { 
    return this._http.get(`${this.actionUrl}${request}`) 
     .map(res => this.extractData(res)) 
     .catch(this.handleError); 
    } 

    private extractData(res: Response) { 
    return res.json(); 
    } 

    private handleError(error: Response) { 
    console.log('Error', error); 
    return Observable.throw(error.json().error || 'Server error'); 
    } 

} 

funds.service.ts

import { Injectable } from '@angular/core'; 
import { HttpService } from './http.service'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class FundsService { 
    constructor(public httpService: HttpService) {} 

    getFunds(): Observable<any> { 
    return this.httpService.get('/addFunds') 
     .map(res => res); 
    } 
} 

funds.component.ts

export class FundsComponent { 
funds: Funds; 

constructor(private fundsService: FundsService) { 
    this.funds = new Funds(); 
} 

onGet() { 
    this.fundsService.getFunds().subscribe(
     data => { 
      console.log(data); 
      this.funds = data; 
     } 
    ); 
    } 
} 

funds.component.html

<input type="text" [(ngModel)]="funds.Bank_Name"> 

希望它有幫助!

+0

如何顯示JSON數據到我的組件模板上...在輸入字段? – chirag

+0

其實你會在你的funds.component.ts中使用非json,但javascript對象 –

相關問題