【问题标题】:Angularjs variable value is not updating first time with sweetAlert is successAngularjs变量值第一次没有更新sweetAlert是成功的
【发布时间】:2017-06-13 13:07:52
【问题描述】:

我使用了 AngularJS v1.5.3 & vendor.bundle.js 和 app.bundle.js

在此单击以显示按钮并显示 swal 确认框,我按下“是的,显示它!”按钮和 div 不显示但是,我第二次按下然后显示这个 div,我的意思是 $scope.displayDiv 值正在改变,但第一次没有任何影响 请给我解决方案

我在这里粘贴我的代码:

<div class="input-group" ng-show="displayDiv" >
        DOM data
</div>
<button class="" ng-click="disDiv()">Show</button>


    $scope.displayDiv=0;
    $scope.disDiv = function() {
            swal({
                title: 'Are you sure?',
                text: "You won't to display",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, display it!',
                cancelButtonText: 'No, cancel!',
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                buttonsStyling: false
            }).then(function() {
                $scope.displayDiv=1;
            });
        };

【问题讨论】:

  • 请不要在angularjs相关问题上使用angular标签
  • 成功事件不在 Angular 世界中。用户 $scope.$apply
  • 非常感谢'seyfside',它工作正常......

标签: angularjs sweetalert sweetalert2


【解决方案1】:

尝试以下功能可能会有所帮助。

<div class="input-group" ng-show="displayDiv" >
        DOM data
</div>
<button class="" ng-click="disDiv()">Show</button>


    $scope.displayDiv=0;
    $scope.disDiv = function() {
            swal({
                title: 'Are you sure?',
                text: "You won't to display",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, display it!',
                cancelButtonText: 'No, cancel!',
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                buttonsStyling: false
            }).then(function() {
                $scope.$apply(function () {
                        $scope.displayDiv=1;
                    });
            });
        };

我在 .then 中添加了以下函数

$scope.$apply(function () { $scope.displayDiv=1; });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2016-10-30
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多