简体   繁体   English

如何在 Angular 中的另一个 function 中调用 function

[英]How to call a function inside another function in Angular

I am building an online store, I am using paystack payment gateway , in the api, I am trying to run a function inside the payment gateway api but it's not working.我正在建立一个在线商店,我正在使用支付堆栈支付网关,在 api 中,我试图在支付网关 api 内运行 function,但它不起作用。 Here is the code:这是代码:

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();
}

in the service page here is the code在服务页面这里是代码

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))
}

Now the issue is that the function completePayment is calling partially, the console.log is running, but its not sending the http request, pls how can i solve this issue现在的问题是 function completePayment正在部分调用,console.log 正在运行,但它没有发送 http 请求,请问我该如何解决这个问题

Write the payWithPaystack method so that it returns a promise:编写payWithPaystack方法,使其返回 promise:

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();
    });
}

Then use the promise that it returns:然后使用它返回的 promise:

this.payWithPaystack(amount)
 .then( sentData => {
    console.log('enter', sentData);
    this.http.post(`${this.ApiURL}payAm.php`, sentData, {headers:this.headers})
    .subscribe(data=>console.log('ok'), error=>console.log(error))
}).catch( reason => {
    console.log("rejected: ", reason);
    this.color='danger'
    this.payDisplay='Transaction closed'    
    this.payClick=false;
});

Promises are the best way to chain asynchronous operations. Promise 是链接异步操作的最佳方式。

Bind the syntax绑定语法

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

You can also try to bind the service ref.您也可以尝试绑定服务引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM