sweetalert 确认在 angularjs 中删除 http 请求无效

sweetalert confirmation on delete in angularjs on http request not working

我是 angularjs 的新手,我在使用 sweetalert 获取警报消息时遇到问题。我的问题是我在单击删除按钮时收到 sweetalert 确认框,但 "yes" 和 "no" 事件在其中不起作用。我只找到了基于 ajax 请求的答案,但我没有在 angularjs 范围内的 httprequest 上找到答案。感谢帮助。提前致谢。

 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");                          
             });  
            }
         });
     };   });

你有错误的 swal 功能部分。

Here's a fixed sample 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
},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");                 
        });
    });
});

修改代码

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");
        });*/
    }
});