I am trying to call a custom javascript alert in typescript. The problem is that I can't use "this" keyword in javascript and typescript code does not wait for javascript function to finish.
What I am saying is:
declare var $: any;
declare var swal: any;
...
delete(id: number) {
var toDeleteOrNot: boolean = false;
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
}, function (isConfirm: boolean) {
if (isConfirm) {
toDeleteOrNot = true;
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
if (toDeleteOrNot) {
this.getagentiiService.deleteAgentie(id).subscribe(x => {
this.populateAgentii();
});
}
}
The function(swal) and the "if" are called at the same time. Var toDeleteOrNot in if maintains its initialization value(in this case it is false). I can't move the "if" inside the javascript because it gives
'this' implicitly has type any because it does not have a type annotation
How can I use this custom javascript function inside typescript?
swalfunction is async, so you have to wait for its callback to execute sotoDeleteOrNothas a value.sweetalertand the function passed to the configuration is an event handler fired after the button click.angulartag, whyangularjsthen? or am I missing something ?