Angular 如何从HTML值更新.ts文件中的值?
我正在使用node的ngx paypal包在我的angular应用程序中实现paypal支付。在我的TS文件中,我可以看到产品价格的值设置在哪里,并且它当前设置为硬编码值“9.99”。我想根据MongoDB中存储的值进行价格更改,我已经能够在HTML页面中成功显示该值,但还无法将该值传递到PayPal API中。我不熟悉使用Angular/MEAN堆栈,我不知道如何获取值并将其传递到API中。谢谢Angular 如何从HTML值更新.ts文件中的值?,angular,paypal,mean-stack,paypal-sandbox,Angular,Paypal,Mean Stack,Paypal Sandbox,我正在使用node的ngx paypal包在我的angular应用程序中实现paypal支付。在我的TS文件中,我可以看到产品价格的值设置在哪里,并且它当前设置为硬编码值“9.99”。我想根据MongoDB中存储的值进行价格更改,我已经能够在HTML页面中成功显示该值,但还无法将该值传递到PayPal API中。我不熟悉使用Angular/MEAN堆栈,我不知道如何获取值并将其传递到API中。谢谢 //.ts file snippit initConfig() { this.payPalConf
//.ts file snippit
initConfig() {
this.payPalConfig = {
currency: 'EUR',
clientId: 'sb',
createOrderOnClient: data =>
<ICreateOrderRequest>{
intent: 'CAPTURE',
purchase_units: [
{
amount: {
currency_code: 'EUR',
value: '9.99',
breakdown: {
item_total: {
currency_code: 'EUR',
value: '9.99'
}
}
},
//html file snippit
.
.
.
<h2 class="animated flipInX delay-2s" id="price" >{{ spotsData.price }}</h2>
.
.
.
移动initconfig后出错
Uncaught Error: /v2/checkout/orders returned status: 400 (Corr ID: ddc5504012afb)
调用initConfig()位于subscribe
外部的code>将立即(同步)调用它,而不是等待subscribe
块运行。subscribe
块将异步运行,仅当从API检索到数据时
您将在subscribe
块中获得数据。因此,理想情况下,您应该在将this.spotsData
设置为data
后调用initConfig
尝试一下:
ngOnInit() {
setTimeout(this.startMap, 2000);
let id = this.route.snapshot.params['id'];
this.selectId = id;
this.spotsData = [];
this._chosenSpotService.getSpots(this.selectId).subscribe((data: any) => {
console.log(this.spotsData);
this.spotsData = data;
initConfig();
});
}
然后在initConfig()
方法中:
initConfig() {
this.payPalConfig = {
currency: 'EUR',
clientId: 'sb',
createOrderOnClient: data => <ICreateOrderRequest> {
intent: 'CAPTURE',
purchase_units: [{
amount: {
currency_code: 'EUR',
value: this.spotsData.price,
breakdown: {
item_total: {
currency_code: 'EUR',
value: this.spotsData.price
}
}
},
...
}
initConfig(){
this.payPalConfig={
货币:欧元,
clientId:'某人',
createOrderOnClient:data=>{
意图:“捕获”,
购买单位:[{
金额:{
货币代码:“欧元”,
值:this.spotsData.price,
细分:{
项目u总计:{
货币代码:“欧元”,
值:this.spotsData.price
}
}
},
...
}
请共享从公开MongoDB数据的API中获取sportsData.price
值的代码。刚刚添加了它@siddajmera,您在哪里调用initConfig
?理想情况下,您应该在subscribe
块内调用它。我在subscribe块外立即调用它,I c这是我的第一个angular应用程序,所以我一直在学习。你能分享你从db获得的数据吗?当我尝试这个时,我得到一个错误400,在paypal窗口启动后,它就崩溃了。你有任何错误吗?如果有,请在问题中分享。根据沙盒环境本身的间歇性问题,而不是你的代码有问题。你可能想稍后再试一次。希望到时候它能正常工作。在那个线程中,OP的问题似乎在几天内就解决了,除此之外,我还没有发现代码有任何问题,但每当我试图通过这个.spotsData.price,f传递值时,都会遇到这个问题或者1-2个月。我从未使用过stackblitz,但我可以给你寄任何有用的东西
initConfig() {
this.payPalConfig = {
currency: 'EUR',
clientId: 'sb',
createOrderOnClient: data => <ICreateOrderRequest> {
intent: 'CAPTURE',
purchase_units: [{
amount: {
currency_code: 'EUR',
value: this.spotsData.price,
breakdown: {
item_total: {
currency_code: 'EUR',
value: this.spotsData.price
}
}
},
...
}