Sweetalert: 确认功能不工作

创建于 2014-10-10  ·  9评论  ·  资料来源: t4t5/sweetalert

您好,我之前成功使用过 SweetAlert,但今天我尝试将它与 Ionic 一起使用,但遇到了问题。
当我像这样使用它时,它会执行 something 函数,但它不会等待按下确认按钮:

$scope.something=function(){console.log('test')};
swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },$scope.something());

当我这样使用它时,它根本不起作用:

swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },function(){console.log('test')});

有任何想法吗?

最有用的评论

控制台日志应该可以工作,但以防万一。 请记住,您需要调用 Angular $scope.$digest(); 如果您使用其他库来更新视图和范围。

因此,如果您使用的是甜蜜警报,请确保您的回调如下所示:

$scope.something=function(){
  console.log('test');
  // You might have something that updates your scope here
  ...
  $scope.$digest();
};
swal({
    title: 'True',
    text: '',
    type: 'success',
    confirmButtonText: 'Next' 
  }, $scope.something);

所有9条评论

我遇到过类似的角度。

$scope.deleteSomething = function () {
    swal({   title: 'Are you sure?', text: 'Your will not be able to recover this thing!',
                type: 'warning',
                showCancelButton: true, confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, delete it!', closeOnConfirm: false },
                function () {
                    // Code to delete something
                    swal('Deleted!', 'It was deleted.', 'success');
                   // The success alert only shows for a split second and is gone
                }
          });

似乎与将它放在作用域中调用的函数中或仅将它放在 callbak 中有关,但我知道什么:)

如果我弄清楚了,我会在这里发布。 看起来这里可能发生了类似的事情: https :

可能是错误的,但看起来您需要删除 $scope.something() 上的括号作为回调,例如

swal({title: 'True',text: '',type: 'success',confirmButtonText: 'Next' },$scope.something);

当您包含括号时,JS 解释器正在执行 $scope.something() 并将结果分配为回调,因此它不等待按下确认按钮。 在您的第二个示例中,您正在传递函数定义(与仅传递 $scope.something 不带括号的内容相同),这就是它起作用的原因。

谢谢,但我已经尝试过了,但它什么也没做

@schris12检查ngSweetAlert 。 也许它可以帮助

控制台日志应该可以工作,但以防万一。 请记住,您需要调用 Angular $scope.$digest(); 如果您使用其他库来更新视图和范围。

因此,如果您使用的是甜蜜警报,请确保您的回调如下所示:

$scope.something=function(){
  console.log('test');
  // You might have something that updates your scope here
  ...
  $scope.$digest();
};
swal({
    title: 'True',
    text: '',
    type: 'success',
    confirmButtonText: 'Next' 
  }, $scope.something);

@oitozero - ngSweetAlert 很有魅力! 谢谢!

@oitozero ,ngSweetAlert 也为我工作。 谢谢!
也感谢@blaiprat提供提示并帮助理解问题。

@SeanPlusPlus @ruairitobrien
惊人的。 :+1:

刚刚发布了一个新版本以反映 sweetalert (0.2.0) 的变化。

非常感谢。
几乎没有,甚至是一个很大的“但是”——如果您在活动建模窗口中调用,按钮将不起作用。
请告诉我如何摆脱祸害?

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

daftspunk picture daftspunk  ·  4评论

mouro001 picture mouro001  ·  3评论

rafatux picture rafatux  ·  5评论

xgqfrms-GitHub picture xgqfrms-GitHub  ·  4评论

Untit1ed picture Untit1ed  ·  5评论