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
source to share
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.
source to share