从甜警报确认对话框响应 [英] Response from Sweet-alert confirm dialog

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

问题描述

我有我的地方我costumize甜警告对话框的功能。我想用它在很多地方,因此设置像一个函数:

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?

推荐答案

这听起来像你想基于不同的行为,如果用户presses确认键或取消按钮。

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文档

Here's an example straight form the 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");
        }
    }
);

在这个例子中,当确认键为pressed,新SweetAlert被打开,确认您的行动,如果取消按钮是pressed,新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.

由于该库是使用异步回调,没有从返回值SwaI位方法。

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

此外,它很可能是好主意,使用图书馆,如纳克-sweet警报包甜警报电话,以确保在警报甜回调所做的任何更改都正确的角度生命周期回升。如果检查出NG-甜警报的来源,你会看到作者换到呼叫SwaI位键,用户在 $回调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.

从code风格的角度来看,这将是更好地把你的逻辑转化为重用的服务或工厂整个codeBase的,而不是仅仅将其连接到$ 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.

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

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