如何从angular 2 +(razorpay)的回调内部调用组件函数?

问题描述 投票:0回答:1

[因此,我尝试从api上的回调响应中为付款应用程序razorpay调用函数。我不能使用'this'来调用组件内的函数,因为它位于处理程序中的嵌套函数内。如何从回调“ handler”调用函数handle_response()?

myComponent.ts

var options = {
    "amount": 100, 
    "name": "ABC",
    "currency": "USD",
    "handler": function (response){
        console.log(response);//this returns the expected value
        this.handle_response(response); //does not work as cannot identify 'this'
    }
};
var rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();

handle_response(_response){....}
angular callback razorpay
1个回答
1
投票

您想使用函数的bind方法或打字稿中的粗箭头语法。可能是:

let options = {
    "amount": 100, 
    "name": "ABC",
    "currency": "USD",
    "handler": function (response){
        console.log(response);//this returns the expected value
        this.handle_response(response); //does not work as cannot identify 'this'
    }.bind(this)
};
let rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();

handle_response(_response){....}

OR

let options = {
    "amount": 100, 
    "name": "ABC",
    "currency": "USD",
    "handler": (response) => {
        console.log(response);//this returns the expected value
        this.handle_response(response); //does not work as cannot identify 'this'
    }
};
let rzp1 = new this.winRef.nativeWindow.Razorpay(options);
rzp1.open();

handle_response(_response){....}
© www.soinside.com 2019 - 2024. All rights reserved.