Variable variable - Error while assigning a variable to a variable

How to access a variable from a function callback. I will show that an example will be more direct in the question

export class FormComponent {

  public pedidoSoft:PedidoSoft = new PedidoSoft();

  getBrandCard(){

       PagSeguroDirectPayment.getBrand({

            cardBin: this.pedidoSoft.numCard,
            success: function(response) {

                    this.pedidoSoft.bandCard = response.brand.name;

            },
            error: function(response) { },
            complete: function(response) { }
       });  

   }

      

I am getting the following error. This error occurs when this.pedidoSoft.bandCard gets response.brand.name

enter image description here

+3


source to share


1 answer


Don't use function

in TypeScript. Replace them with operators instead ()=>{}

.

        success: (response) => {
                this.pedidoSoft.bandCard = response.brand.name;
        },
        error: (response) => { },
        complete: (response) => { }

      



When you use function() {}

, this

it is not permanent where the ()=>{}

link is stored this

.

Your alternative is to use bind(this)

for functions.

+4


source







All Articles