Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inside jquery function can i call angular 5 function

Tags:

angular

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

like image 496
Ashok Avatar asked Mar 14 '18 19:03

Ashok


People also ask

How do you call a function within a function in jQuery?

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

Can jQuery be used with angular?

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.

Can jQuery call a JavaScript function?

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!


1 Answers

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()
like image 162
Shane Avatar answered Nov 15 '22 11:11

Shane