【发布时间】:2018-03-24 22:57:01
【问题描述】:
我刚刚开始处理 node 和 angular,我无法将我的 nodejs 文件中的 JSON 数据传递给我的 angular 组件并通过单击按钮将其呈现在屏幕上。谢谢你的帮助。
这是我要传递的 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)
);
}
}
【问题讨论】:
-
为什么要在 mapper 中创建 const?你可以返回 response.json()
-
好吧..我正在返回分配给 response.json() 的数据