mardi 9 novembre 2021

Typescript conditional code is running regardless of confirm() value

I am trying to implement a dialogue box that confirms with the user if they want to continue with an action.

deleteItem(index: number): void {
  let response = window.confirm("Are you sure?");
  if (response) {
      // delete item code
      this.item.splice(index,1);
      console.log("A");
  } else {
      console.log("B");
  }
}

When I click the deleteItem button to trigger the dialogue, it deletes the item regardless but prints the correct console.log() string. Can anyone help me understand why this is happening and how to correct this?

Are you sure? "Ok" => deletes the item and logs "A" in console.

Are you sure? "Cancel" => deletes the item and logs "B" in console.

Aucun commentaire:

Enregistrer un commentaire