Conflicts between Ajax and angular 2

0

I am sending a form with AJAX and everything works fine, the problem is that I need to store the AJAX response in an Angular2 variable.

My code:

xhr.onreadystatechange = function(e) {
    this.miVariableAngular = this.responseText; 
};

Error:

The error arises because the program believes that when using this.myVariableAngular I am calling an AJAX method, since it confuses the THIS variable variable with the THIS reference to the AJAX response .

How can I assign that answer in a variable of angular 2?

I thank you in advance for your collaboration,

    
asked by Deo Vidal 01.12.2017 в 22:43
source

1 answer

0

You must make onreadystatechange an arrow function:

xhr.onreadystatechange = (e => {
  this.miVariableAngular = this.responseText; 
})
    
answered by 06.12.2017 в 18:12