【问题标题】:Callback function not being called - SweetAlert未调用回调函数 - SweetAlert
【发布时间】:2017-11-29 06:31:18
【问题描述】:

我正在根据 if 语句的结果调用 sweetalert。

    for (int i = 0; i < count; i++) {
        if (numValue == 0) {
            swal({
                    title: 'Warning',
                    text: "Hey there..!!",
                    type: 'warning',
                    showCancelButton: true,
                    confirmButtonText: 'Yes',
                    cancelButtonText: 'No',
                    confirmButtonClass: 'confirm-class',
                    cancelButtonClass: 'cancel-class',
                    allowOutsideClick: false
                }, function(isConfirm) {
                    if (isConfirm) {
                        console.log("isConfirm in if: " + isConfirm);
                        $scope.$apply(function() {
                            $scope.saveAfterPSConfirm();
                        });
                    } else {
                        console.log("isConfirm in else: ");
                        return;
                    }
                }


            );
        }
    }

如果 numValue 为 0,这将调用警告甜蜜警报。例如,numValue 为 0。

当调用这个 sn-p 时,甜蜜的警报警告消息“嘿那里..!!”来但立即解散,无需等待任何按钮单击 numValue 弹出窗口。 问题是没有调用 numvalue sweet alert 的回调函数。

【问题讨论】:

  • 不清楚哪里出了问题第一个返回为什么第一个回调中没有第二个?
  • @shaunhusain 因为第一个 swal 调用和第二个 swal 调用没有任何关系......另外,我的问题是 swal 的回调没有被调用。
  • 如果您只是将问题减少到最少的复制,以免混淆问题,则可以提供帮助。听起来你说在这两种情况下回调都没有被触发,你是否尝试在回调函数中添加断点或debugger 语句。
  • 我确实添加了调试器,并意识到回调没有被触发。看起来有一个警报弹出窗口,并且立即有另一个警报弹出窗口。理想情况下,我希望第一个弹出窗口等待单击某个按钮(确认/取消)并相应地执行回调。
  • 这就是为什么我说你希望在第一个回调函数中调用第二个 swal,一般来说 JS 不会因为添加了一些 UI 元素而阻塞视图函数将继续执行语句(本机 window.alert 或 window.prompt 是规则的例外,但不是规则)。关于不触发回调,虽然听起来像是 swal 代码中的一个问题,但 Angular 中的任何内容都不应阻止它,请尝试在调试面板中启用“异常暂停”以查看它是否遇到了一些捕获的异常。

标签: jquery angularjs sweetalert sweetalert2


【解决方案1】:

我带来了一个基于你的代码的小提琴。

这个小提琴使用了SweetAlert2。

Check this

在我的项目中,我使用了return swal(),所以我将你的代码应用到它上面,效果很好。

代码如下:

swal({
    title: 'Warning',
    text: "Hey there..!!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Yes',
    cancelButtonText: 'No',
    confirmButtonClass: 'confirm-class',
    cancelButtonClass: 'cancel-class',
    allowOutsideClick: false
}).then(function(isConfirm) {
    if (isConfirm) {
        console.log("isConfirm in if: " + isConfirm);
        $scope.$apply(function() {
            //$scope.saveAfterPSConfirm();
        });
        count += 1;
        if(count < 3){           // input count.length instead of 3
            $scope.callAlert(count);
        }
    } else {
        console.log("isConfirm in else: ");
        return;
    }
});

我使函数递归,这意味着该函数将调用自身直到count.length。

希望我的解决方案对您有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多