So I am trying to call a function from within a callback response on an api for a payment app razorpay. I cannot use 'this' to call functions within the component as it is within a nested function in the handler. How can I call the function handle_response() from the callback "handler"?
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){....}
You want to use the bind
method of your function or the fat arrow syntax from typescript. It would be either:
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 you can use javascript arrow function. read more about arrow function here
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){....}