在http请求上的angularjs中删除sweetalert确认无法正常工作

时间:2017-12-29 06:56:07

标签: angularjs angularjs-controller sweetalert

我是angularjs的新手,我遇到的问题是我使用sweetalert作为警报信息。我的问题是我在点击删除按钮时得到sweetalert确认框,但是"是"和"不"事件在其中无效。我发现只有基于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");                          
             });  
            }
         });
     };   });

1 个答案:

答案 0 :(得分:1)

你有错误的功能部分。

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