如何将ngModel值传递给Angular 6中的组件

时间:2019-05-09 10:29:38

标签: angular paypal angular6

我正在从事Angular 6电子商务项目,并将PayPal付款集成到该项目中。我在HTML中添加了paypal按钮,在[(ngModel)]中获取金额,但是,我必须将其传递到组件文件中,以便可以在paypal配置中读取它。任何更好或替代的解决方案都受到高度赞扬

以下是文件:

  1. shopping-cart-summary.html
<div *ngIf="cart$ | async as cart">
  <input type="number" [(ngModel)]="cart.totalPrice">
  <div id="paypal-checkout-btn"></div> 
</div>
  1. 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的价值,我想在组件文件中传递它,那么该怎么做?有什么建议吗?

这是产品总价的屏幕截图 enter image description here

2 个答案:

答案 0 :(得分:0)

代替totalPrice:数字;在您的component.ts中使用cert = {“ totalPrice”:0}; 否则,在component.html中使用totalPrice而不是cert.totalPrice。

答案 1 :(得分:0)

由于cart$是可观察的,因此您将需要订阅它来获取值,或者在可管道运算符中使用它。

尝试一下:

cart$: Observable<any>;
totalPrice: number;

public ngAfterViewChecked(): void {
    const elementExists: boolean = !!document.getElementById('paypal-checkout-btn');

    cart$.subscribe((cart: any) => {
        this.totalPrice = cart.totalPrice;
    })

  if(elementExists && !this.addScript && this.totalPrice) {
    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;
    });
}