I have an Angular4 app which calls a service to post a record using my api.
When theAssignmentChanged
value is true
, it's going to send a text message. When false
, it just skips over it.
In the following code, the console.log
shows theAssignmentChagned
flag as true
, but the console.log
inside the call to the service shows it as false
.
I'm losing scope of this
and I've looked at stack exchange and haven't found anything that's explaining why.
Why am I losing the scope of this
inside my call to FService
?
console.log("Before : " + myForm.controls.theAssignmentChanged.value);
this.FService.reqExec(formToPost, 'service', action)
.then( data => {
this.notify.success('Success','DB Update Successful!');
console.log(" After : " + myForm.controls.theAssignmentChanged.value);
if (myForm.controls.theAssignmentChanged.value) {
this.messageService.sendMessage(telToText, 'Hi, the service has been changed, please review the changes.');
}
this.closeModal('services');
resolve(data);
})
.catch(errorCode => {
console.log(errorCode);
reject(errorCode);
});
} else {
this.closeModal('services');
resolve(data);
}
});
})
this is different than the javascript accessing this inside of then because I tried that and it didnt work, and also... when this function was not in a service, it worked fine. It only started happening when I took it out of the parent component and placed it inside the child service.
Thanks everyone... what was happening was a change in scope of this. And yes, this.notify worked, what was happening was an async condition occurred after the call to the procedure where the scope of this was turned to null because of a break in a follow on procedure. It just happened to be timed with right after this.notify and before the next line. I added async await and it works as normal now.