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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44973038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:50:00  来源:igfitidea点击:

how to close sweet alert on ajax request completion

javascriptjqueryangularjsajaxsweetalert

提问by Kgn-web

I am using Sweet-alertin my angular app.

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

    }

Req #1 (Main Objective of my this post)

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

What I am looking for is when the ajax request completes i.e., controls enters in the then(), Sweet alert should automatically hide.

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

Req #2 Also while request processing, I don't want to have the Close pop-up button (Ok button) in the sweet alert.

请求 #2 同样在处理请求时,我不想在甜蜜警报中有关闭弹出按钮(确定按钮)。

enter image description hereAs per the documentation,showConfirmButton: falseshould hide it but it's not.

在此处输入图片说明根据文档,showConfirmButton: false应该隐藏它,但事实并非如此。

Any help/suggestion highly appreciated.
Thanks.

任何帮助/建议高度赞赏。
谢谢。

回答by Eric Hodonsky

For automatically hiding the pop-over when it's done, you should set your initial pop-over to a variable so you can access it later. Maybe:

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

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

}

It's right on: https://t4t5.github.io/sweetalert/in the methods section near the bottom.

就在:https: //t4t5.github.io/sweetalert/靠近底部的方法部分。

Since you don't have a specific 'way' you want to do hide the ok button and you're just looking for suggestions, you could always just use a little CSS to target it and give it the ol display: none;setup.

由于您没有想要隐藏 ok 按钮的特定“方式”,而您只是在寻找建议,因此您始终可以使用一点 CSS 来定位它并为其提供 oldisplay: none;设置。

回答by sur97c

You can use the close method over the sweet object see the documentation in down part

您可以在甜蜜的对象上使用 close 方法,请参阅下方的文档

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

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

swal.close(); --> Close the currently open SweetAlert programmatically.

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

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

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

回答by Angrej Kumar

You can close current showing sweetalert by using below line of code anywhere you want.

您可以通过在您想要的任何位置使用以下代码行来关闭当前显示的 Sweetalert。

swal.close();

That's it!

就是这样!

回答by erdimeola

SweetAlert has close method if you check the docs at http://t4t5.github.io/sweetalert/

如果您查看http://t4t5.github.io/sweetalert/ 上的文档,SweetAlert 有关闭方法

You can use SweetAlert.close()to close the sweetalert in angular.

您可以使用SweetAlert.close()angular 关闭sweetalert。

回答by Mario Villanueva

swaldoes not work with syncfunction (ex. get), you need make call getasync

swal不适用于sync函数(例如get),您需要 make call getasync

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

回答by user1419261

if you are using the AngularJS library known as angular-sweetalert then use swal.close(); to close the alert window. angular-sweetalert is a wrapper on the core sweetalert library package.

如果您正在使用称为 angular-sweetalert 的 AngularJS 库,则使用 swal.close(); 关闭警报窗口。angular-sweetalert 是核心sweetalert 库包的包装器。