【发布时间】:2019-05-09 10:29:37
【问题描述】:
我正在开发 Angular 6 电子商务项目并将 PayPal 支付集成到其中。我已经在 html 中添加了 paypal 按钮,我在 [(ngModel)] 中获得了金额,但是,我必须将它传递到组件文件中,以便可以在 paypal 配置中读取它。任何更好或替代的解决方案都受到高度赞扬
以下是文件:
- shopping-cart-summary.html
<div *ngIf="cart$ | async as cart">
<input type="number" [(ngModel)]="cart.totalPrice">
<div id="paypal-checkout-btn"></div>
</div>
- shopping-cart-summary.ts
totalPrice: number;
public ngAfterViewChecked(): void {
const elementExists: boolean = !!document.getElementById('paypal-checkout-btn');
if(elementExists && !this.addScript) {
this.addPaypalScript().then(() => {
paypal.Button.render({
client: {
sandbox: 'My sandbox API',
},
payment: (data, actions) => {
return actions.payment.create({
payment: {
transactions: [
{
amount: {
total: this.totalPrice,
currency: 'USD'
},
payee:{email:'My Email ID'},
}
]
}
});
},
commit: true,
locale: 'en_US',
style: {
size: 'medium', // tiny, small, medium
color: 'blue', // orange, blue, silver
shape: 'pill' // pill, rect
},
env: 'sandbox', // Optional: specify 'sandbox' or 'production'
onAuthorize: (data, actions) => {
return actions.payment.execute().then((payment) => {
console.log('payment 1 completed!');
});
},
onCancel: (data) => {
console.log('payment 1 was cancelled!');
}
}, '#paypal-checkout-btn');
this.paypalLoad = false;
});
}
}
在这里,我在 [(ngModel)] 中获得的价值为 182 美元,我想将它传递到组件文件中,那么该怎么做呢?有什么建议吗??
【问题讨论】:
-
“我想在组件文件中传递”是什么意思?
-
我的意思是如何在组件文件中传递 ngModel 的 cart.totalprice,我得到 $182
-
您需要订阅
cart$才能获得totalPrice 的值,除非您将cart$的值通过管道传递到this.addPaypalScript()。 -
购物车$中没有订阅方法。当我做 console.log(this.cart$) 我得到: Observable {_isScalar: false, source: Observable, operator: MapOperator}