2017-12-29 5 views
1

Ich bin neu zu angularjs, ich habe ein Problem, wo ich Sweetalert für Warnmeldungen verwenden. Mein Problem hier ist, dass ich das sweetalert Bestätigungsfeld auf Löschungstaste klicke, aber "ja" und "nein" Ereignisse arbeiten nicht darin. Ich habe nur Antworten gefunden, die auf einer Ajax-Anfrage basieren, aber ich habe nicht auf httprequest innerhalb des angularjs-Bereichs gefunden. Hilfe wird geschätzt. Danke im Voraus.sweetalert Bestätigung auf delete in angularjs auf http Anfrage funktioniert nicht

var app = angular.module("myapp", ['sweetalert']) 
app.controller("ProductController", function ($scope, $http) { 
    $scope.delete = function (qid) { 
     swal({ 
      title: "Are you sure?", 
      text: "Your 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, 
      showLoaderOnConfirm: true 
     }, 
    function(isConfirm){ 
     if (!isConfirm) { 
      swal("Cancelled", "Your imaginary file is safe :)", "error"); 
     }else{ 
      $http(httpreq).then(function (data) { 
       var httpreq = { 
        method: 'POST', 
        url: 'Product.aspx/delete', 
        headers: { 
         'Content-Type': 'application/json; charset=utf-8', 
         'dataType': 'json' 
        }, 
        data: { qid: qid } 
       } 
       swal("Deleted!", "Your imaginary file has been deleted.", "success");       
      }); 
      } 
     }); 
    }; }); 

Antwort

1

Sie haben einen falschen Funktionsteil von swal.

Here's a fixed sample code.

Ändern Sie den Code wie folgt aus.

Originalcode

swal({ 
    title: "Are you sure?", 
    text: "Your 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){ 
    if (!isConfirm) return; 
    $http(httpreq).then(function (data) { 
     var httpreq = { 
      method: 'POST', 
      url: 'Product.aspx/delete', 
      headers: { 
       'Content-Type': 'application/json; charset=utf-8', 
       'dataType': 'json' 
      }, 
      data: { qid: qid } 
     } 
     swal("Deleted!", 
      "Your imaginary file has been deleted.", 
      "success"); 
     }).catch(function (error) { 
      swal("Cancelled", 
        "Your imaginary file is safe :)", 
        "error");     
     }); 
    }); 
}); 

Modified-Code

swal({ 
    title: "Are you sure?", 
    text: "Your 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, 
    showLoaderOnConfirm: true   // Add this line 
}, function(isConfirm){ 
    if (!isConfirm) { 
     swal("Cancelled", "Your imaginary file is safe :)", "error"); 
    } else { 
     // $timeout is sample code. Put your http call function into here instead of $timeout. 
     $timeout(function(){ 
      swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
     },2000); 

     /*$http({ 
      method: 'POST', 
      url: 'Product.aspx/delete', 
      headers: { 
       'Content-Type': 'application/json; charset=utf-8', 
       'dataType': 'json' 
      }, 
      data: { qid: qid } 
     }).then(function (data) { 
      swal("Deleted!", "Your imaginary file has been deleted.", "success"); 
     });*/ 
    } 
}); 
+0

ich das versucht, funktioniert gut, wenn ich auf Abbrechen klicken, aber wenn ich löschen klicken, wird es nicht funktioniert. Schau dir meinen neuen Schnitt an, wie ich es versucht habe. –

+0

@karthikshankar Warum haben Sie die Option von "http" in diesem erklärt? Hat das lange vorher funktioniert? Versuchen Sie, 'var httpreq = {...}' aus 'http' wie meinen modifizierten Code zu verschieben. –

+0

Vielen Dank @Canet Robern i) Ich habe es falsch erklärt. ii) Nein, das funktioniert nicht, da ich die Variable vor der Deklaration benutzt habe. iii) Ja, ich habe 'var httpreq = {...}' aus '$ http (httpreq)' bewegt und es hat funktioniert. –

Verwandte Themen