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

sweetalert确认http请求上angularjs中的删除无效,angularjs,angularjs-controller,sweetalert,Angularjs,Angularjs Controller,Sweetalert,我是angularjs的新手,我在使用sweetalert发送警报消息时遇到了一个问题。我这里的问题是,我在单击删除按钮时得到了sweetalert确认框,但是“是”和“否”事件在其中不起作用。我只找到了基于ajax请求的答案,但在httprequest上没有找到angularjs范围内的答案。谢谢你的帮助。提前谢谢 var app = angular.module("myapp", ['sweetalert']) app.controller("ProductController", fu

我是angularjs的新手,我在使用sweetalert发送警报消息时遇到了一个问题。我这里的问题是,我在单击删除按钮时得到了sweetalert确认框,但是“是”和“否”事件在其中不起作用。我只找到了基于ajax请求的答案,但在httprequest上没有找到angularjs范围内的答案。谢谢你的帮助。提前谢谢

 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功能部分

像这样更改代码

原始代码

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");
        });*/
    }
});
修改代码

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

我尝试了这个,当我单击“取消”时工作正常,但当我单击“删除”时,它不工作。查看我的新编辑,我是如何尝试的。@karthikshankar为什么要在其中声明“http”选项?那以前有效吗?尝试将
var-httpreq={…}
从“http”中移出,就像我修改的代码一样。非常感谢@Canet Robern i)我错误地声明了。ii)不,这不起作用,因为我在声明之前使用了变量。iii)是的,我将
var-httpreq={…}
$http(httpreq)
中移出,它成功了。@karthikshankar对你很好,它成功了。:)祝您有个美好的一天。