Sweet-alert 确认对话框的响应 [英] Response from Sweet-alert confirm dialog

查看:39
本文介绍了Sweet-alert 确认对话框的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,我可以将我的甜蜜警报对话框装扮起来.我想在很多地方使用它,因此将其设置为如下函数:

I have a function where i costumize my sweet-alert dialog. I want to use it in a lot of places and therefore set that in a function like:

$rootScope.giveConfirmDialog = function(title,text,confirmButtonText,toBeExecFunction){
        swal({title: title,   
        text: title,
        .....
        confirmButtonText: confirmButtonText }, 
        toBeExecFunction);
    }

我想做的很简单:在某处调用该函数并根据用户的回答继续,因此:

What i want to do is simple: calling that function in somewhere and go on based on the answer of the user, thus:

var res = $scope.$root.giveConfirmDialog("..",
                "test", "test", function () {
                return true;
            });

但我不接受任何回应.实际上,我找不到这样的例子,我认为这不是常见的使用方式.但怎么可能呢?

But i don't take any response. Actually, i couldn't find such an example and i think its not the common way of use. But how can it be possible?

推荐答案

听起来您想要根据用户按下确认按钮还是取消按钮而获得不同的行为.

It sounds like you want different behavior based on if the user presses the confirm button or the cancel button.

SweetAlert 通过回调函数上的参数公开用户响应.

SweetAlert exposes the user response via a parameter on the callback function.

以下是 SweetAlert Docs 的示例:

swal({
        title: "Are you sure?",
        text: "You 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) {
            swal("Deleted!", "Your imaginary file has been deleted.", "success");
        } else {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
    }
);

在本例中,当按下确认按钮时,会打开一个新的 SweetAlert,确认您的操作,如果按下取消按钮,则会打开一个新的 SweetAlert,注意操作已取消.您可以使用您需要的任何功能替换这些调用.

In this example, when the confirm button is pressed, a new SweetAlert is opened, confirming your action, and if cancel button is pressed, a new SweetAlert is opened, noting that the action was cancelled. You can replace these calls with whatever functionality you need.

由于此库使用异步回调,因此 swal 方法没有返回值.

Since this library is using async callbacks, there is not a return value from the swal method.

此外,使用诸如 ng 之类的库可能是个好主意-sweet-alert 包装对甜蜜警报的调用,以确保您在甜蜜警报回调中所做的任何更改都被角度生命周期正确接收.如果您查看 ng-sweet-alert 的源代码,您会看到作者将调用 swal 和用户的回调封装在 $rootScope.$evalAsync 中,确保在调用和回调完成时角度更新.

Also, it would probably be good idea to use a library such as ng-sweet-alert to wrap calls to sweet alert to ensure that any changes you make in Sweet Alert callbacks are properly picked up by the angular lifecycle. If you check out the source for ng-sweet-alert, you'll see that the author wraps the calls to swal and the user's callback in $rootScope.$evalAsync, ensuring that angular updates when the calls and callbacks complete.

从代码风格的角度来看,最好将您的逻辑放入服务或工厂中以便在整个代码库中重用,而不是仅仅将其附加到 $rootScope.

From a code style perspective, it would be better to put your logic into a service or factory for reuse throughout your codebase rather than just attaching it to $rootScope.

这篇关于Sweet-alert 确认对话框的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆