如何关闭ajax请求完成时的甜蜜警报

问题描述 投票:0回答:9

我在我的 Angular 应用程序中使用

Sweet-alert

function GetDataFromServer(url) {
        SweetAlert.swal(
    {
        title: "",
        text: "Please wait.",
        imageUrl: "../../app/app-img/loading_spinner.gif",
        showConfirmButton: false
    });
        return $http.get(url)
        .then(success)
        .catch(exception);
        function success(response) {
            //SweetAlert.swal(
            //  {
            //      title: "",
            //      text: "data loaded",
            //  });
            return response.data;
        }
        function exception(ex) {
            return (ex);
        }

    }

需求#1(我这篇文章的主要目标)

我正在寻找的是ajax请求何时完成,即 控件进入then(),Sweetalert应该会自动隐藏。

要求#2 另外,在处理请求时,我不想在甜蜜警报中显示“关闭”弹出按钮(“确定”按钮)。

enter image description here 根据文档,

showConfirmButton: false
应该隐藏它,但事实并非如此。

非常感谢任何帮助/建议。
谢谢。

javascript jquery angularjs ajax sweetalert
9个回答
41
投票

为了在完成后自动隐藏弹出窗口,您应该将初始弹出窗口设置为一个变量,以便以后可以访问它。也许:

function GetDataFromServer(url) {
    SweetAlert.swal({
        title: "",
        text: "Please wait.",
        imageUrl: "../../app/app-img/loading_spinner.gif",
        showConfirmButton: false
    });
    return $http.get(url)
    .then(success)
    .catch(exception);
    function success(response) {
        swal.close()
        return response.data;
    }
    function exception(ex) {
        return (ex);
    }

}

它就在:https://t4t5.github.io/sweetalert/,位于底部附近的方法部分。

由于您没有想要隐藏“确定”按钮的特定“方式”,并且您只是在寻找建议,因此您始终可以使用一些 CSS 来定位它并为其提供 ol

display: none;
设置。


36
投票

您可以在任何您想要的地方使用下面的代码行来关闭当前显示的 sweetalert。

swal.close();

就是这样!


9
投票

您可以对 sweet 对象使用 close 方法,请参阅下面部分的文档

https://t4t5.github.io/sweetalert/

swal.close(); --> 以编程方式关闭当前打开的 SweetAlert。

self.showProgress = function(message) {
  swal({ title: message });
  swal.showLoading();
};

self.hideProgress = function() {
  swal.close();
};

5
投票

如果您使用 swal2,您可以在代码中的任何位置使用

Swal.close()
关闭它,以便在 ajax 完成时关闭它我认为下面的代码是一种简单的方法:

$(document).ajaxComplete(function () {
        Swal.close();
});

4
投票

SweetAlert 有 close 方法,如果你检查文档http://t4t5.github.io/sweetalert/

您可以使用

SweetAlert.close()
关闭 Angular 中的 sweetalert。


4
投票

swal
不适用于
sync
函数(例如
get
),您需要进行调用
get
异步

swal({
    type: 'warning',
    text: 'Please wait.',
    showCancelButton: false,
    confirmButtonText: "ok",
    allowOutsideClick: false,
    allowEscapeKey: false
}).then(function (result) {
    if (result) {

        setTimeout(function () {
            $http.get(url)
        }, 500);
    }
});

2
投票

如果您使用的是 AngularJS 库 Angular-sweetalert,那么请使用 swal.close();关闭警报窗口。 angular-sweetalert 是核心 sweetalert 库包的包装器。


0
投票

缓存

swal()
以便稍后触发。

function GetDataFromServer(url) {

let swalAlert = SweetAlert.swal; // cache your swal

swalAlert({
    title: "",
    text: "Please wait.",
    imageUrl: "../../app/app-img/loading_spinner.gif",
    showConfirmButton: false
});
return $http.get(url)
.then(success)
.catch(exception);
    function success(response) {
        swalAlert.close(); // this is what actually allows the close() to work
        return response.data;
    }
    function exception(ex) {
        return (ex);
    }

}

0
投票

当你想关闭Swal窗口时,可以使用Swal close方法:

Swal.close();
© www.soinside.com 2019 - 2024. All rights reserved.