【问题标题】:How to pass ngModel value to component in Angular 6如何将ngModel值传递给Angular 6中的组件
【发布时间】:2019-05-09 10:29:37
【问题描述】:

我正在开发 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 美元,我想将它传递到组件文件中,那么该怎么做呢?有什么建议吗??

这是产品总价的截图

【问题讨论】:

  • “我想在组件文件中传递”是什么意思?
  • 我的意思是如何在组件文件中传递 ngModel 的 cart.totalprice,我得到 $182
  • 您需要订阅cart$ 才能获得totalPrice 的值,除非您将cart$ 的值通过管道传递到this.addPaypalScript()
  • 购物车$中没有订阅方法。当我做 console.log(this.cart$) 我得到: Observable {_isScalar: false, source: Observable, operator: MapOperator}

标签: angular paypal angular6


【解决方案1】:

由于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;
    });
}

【讨论】:

  • 嘿,非常感谢老兄......终于得到了价值,你拯救了我的一天......
  • 谢谢大佬 我终于完成了这个项目,卡了5天,今天得到了很大的缓解。好吧,最后一件事,我的项目运行良好,但是只有一个小错误:“未捕获的错误:页面上已加载具有相同版本 (4.0.268) 的 PayPal Checkout 集成脚本”。它不会妨碍我的项目流程,但仍然是错误
  • 我需要查看更多代码才能理解错误,无论如何这不会影响您。听起来您在某处/以某种方式多次引用了贝宝脚本。
  • 好的,再次感谢...我会再经历一遍...尝试自己解决。
【解决方案2】:

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

【讨论】:

  • 组件文件中不能传递ngModel的值?
  • 组件文件是什么意思。
  • component.ts 中的组件文件...从 ngModel 获取值并将其传递给 component.ts 中定义的 totalPrice
  • 为了使双向绑定有效,对象应该在ts和html文件中一起存在。我在您的代码中看不到这一点。尽可能简单。在ts上加一个变量,在html中使用。你会实现你想要的。
猜你喜欢
  • 2019-02-15
  • 2021-02-09
  • 2017-05-12
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 2019-03-07
  • 2019-06-10
  • 1970-01-01
相关资源
最近更新 更多