I'm having some difficulties to call a function within a promise and it is certainly related to the scope of the function. When the promise executes and is resolved, an error shows up saying that the function is undefined.
To summarize this code: the ok() function closes a modal after a response from the server is received. And the UserDetailsModel is a service that I have injected in the UserdetailsController.
So is it possible to know what kind of solutions there are for this particular case as I just want to close a modal after the promise is resolved, but I am not very versed with angular and ES6?
Thanks in advance for any suggestions.
class UserDetailsController {
constructor(UserDetailsModel){
this.UserDetailsModel = UserDetailsModel;
};
//the ok function close a modal
ok = function () {
this.close();
};
//creates a json object with the POST method with angular $resource
this.UserDetailsModel.sendMessage().save({name :this.request.fullName,
email:this.request.email})
.$promise.then(function(response, status){
/*****here is undefined*****/
this.ok();
},
function(response) {
message = "Error: "+response.status + " " + response.statusText;
});
}