I am trying to call onPaymentStatus() function inside the jQuery function. But i am unable use 'this' scope
this.braintree.client.create({
authorization: 'client_token'},
(err, client) => {
client.request({
endpoint: 'payment_methods/credit_cards',
method: 'post',
data: data
}, function (requestErr, response) {
if (requestErr) { throw new Error(requestErr); }
console.log('Got nonce:', response.creditCards[0].nonce);
this.onPaymentStatus(response.creditCards[0].nonce); //
});
});
onPaymentStatus (nonce) {
console.log(nonce);
}
I got error ERROR TypeError: Cannot read property 'onPaymentStatus' of null
function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });
jQuery is a small yet feature-rich powerful javascript library that helps to manipulate the HTML DOM with less javascript code. We can use jQuery with Angular. There are some situations you come across while building Angular apps that it's absolutely necessary to use a library like jQuery to get something done.
The way to call a JavaScript function from a JQuery file is the same as calling a JavaScript function from a JavaScript file :) This is so because JQuery is a library based from JavaScript. Say, you want to call function foo from a JavaScript file, when the window loads. I hope this helps!
You need to make a reference to the component outside the jQuery function as the meaning of this
changes within it
const component = this;
Then call
component.onPaymentStatus()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With