如何在Angular中的另一个函数内调用函数

时间:2019-11-21 12:28:08

标签: javascript angular

我正在建立一个在线商店,我正在使用paystack payment gateway,在该api中,我试图在支付网关api中运行一个功能,但该功能无法正常工作。这是代码:

payWithPaystack(returnData, amount){
    const binded = this.service.completePayment.bind(this)
    var handler = PaystackPop.setup({
        key: '.......',
        email: this.details.email,
        amount: amount,
        currency: "NGN",
        ref: `ref-${Math.ceil(Math.random() * 10e13)}`, 
        metadata: {
            custom_fields: [{
                display_name: this.details.firstName+' '+this.details.lastName,
                variable_name: "mobile_number",
                value: this.details.phoneNumber
            }]
        },  
        callback(res){
            console.log(res)                
            binded(res);
        },
        onClose(){
            console.log('window closed');
            this.color='danger'
            this.payDisplay='Transaction closed'    
            this.payClick=false
        }
    });
    handler.openIframe();
}
服务页面中的

是代码

completePayment(sentData){
    console.log('enter')
    this.http.post(`${this.ApiURL}payAm.php`, sentData, {headers:this.headers})
    .subscribe(data=>console.log('ok'), error=>console.log(error))
}

现在的问题是,completePayment函数正在部分调用,console.log正在运行,但是未发送http请求,请问如何解决此问题

2 个答案:

答案 0 :(得分:1)

编写# A tibble: 5 x 16 fullVisitorId date visitStartTime visits hits pageviews timeOnSite bounces transactions transactionReve~ newVisits screenviews <chr> <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> 1 224828163958~ 2017~ 1501583974 1 1 1 NA 1 NA NA 1 NA 2 864743638108~ 2017~ 1501616585 1 1 1 NA 1 NA NA 1 NA 3 205583970085~ 2017~ 1501583344 1 1 1 NA 1 NA NA 1 NA 4 075084606534~ 2017~ 1501573386 1 1 1 NA 1 NA NA 1 NA 5 057342716941~ 2017~ 1501651467 1 1 1 NA 1 NA NA NA NA # ... with 4 more variables: uniqueScreenviews <int>, timeOnScreen <int>, totalTransactionRevenue <int>, sessionQualityDim <int> 方法,使其返回promise:

payWithPaystack

然后使用它返回的承诺:

payWithPaystack(amount){
    return new Promise( (resolve,reject) => {
        var handler = PaystackPop.setup({
            key: '.......',
            email: this.details.email,
            amount: amount,
            currency: "NGN",
            ref: `ref-${Math.ceil(Math.random() * 10e13)}`, 
            metadata: {
                custom_fields: [{
                    display_name: this.details.firstName+' '+this.details.lastName,
                    variable_name: "mobile_number",
                    value: this.details.phoneNumber
                }]
            },  
            callback(res){
                console.log(res)                
                //RESOLVE promise
                resolve(res);
            },
            onClose(){
                console.log('window closed');
                //REJECT promise
                reject('window closed');
            }
        });
        handler.openIframe();
    });
}

承诺是链接异步操作的最佳方法。

答案 1 :(得分:0)

绑定语法

...
payWithPaystack(returnData, amount){
    const binded = this.service.completePayment.bind(this);
    let handler = PaystackPop.setup({
    ....
    callback(res){
      binded(res);
    }
...

您也可以尝试绑定服务引用。