【问题标题】:bootstrap modal buttons not firing with single click with event.preventDefault使用 event.preventDefault 单击时不会触发引导模式按钮
【发布时间】:2016-07-04 06:11:56
【问题描述】:

在角度代码中,我有一个代码在 $locationChangeStart 触发时进行验证。我必须调用 event.preventDefault() 来取消它并显示引导模式。但是,我必须使用模态按钮单击两次才能使每个按钮动作生效。下面是代码:

$scope.$on('$locationChangeStart', function (event, next, current) {

    if (skipValidation.skipAllowed($scope.filteredQuestions[0])) {
        //some code here
    }
    else {

        event.preventDefault();

        skipValidation.openModal();
    }
});

openModal() 函数...

this.openModal = function (size, locationChange) {

    var modalInstance = $uibModal.open({
        animation: true,
        templateUrl: 'skipModalContent.html',
        controller: 'SkipModalInstance',
        size: size,
        resolve: {
        }
    });

    modalInstance.result.then(function () {
        //$log.info('continue');
    }, function () {
    });
};

skipModalContent.html

<script type="text/ng-template" id="skipModalContent.html">
                    <div class="modal-header">
                        <h3 class="modal-title text-warning">Warnung!</h3>
                    </div>
                    <div class="modal-body">
                        Frage ist erforderlich, zu beantworten.
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-default" type="button" ng-click="continue()">mache trotzdem weiter</button>
                        <button class="btn btn-default" type="button" ng-click="cancel()">schließen</button>
                    </div>
                </script>

skipModalInstance 控制器...

var skipModalInstanceCtrl = function ($scope, $uibModalInstance, $window) {

$scope.continue = function () {
    $uibModalInstance.close();
    $window.skipModal = true;
};

$scope.cancel = function () {
    $uibModalInstance.dismiss('cancel');
    $window.skipModal = false;
};
};
app.controller('SkipModalInstance', skipModalInstanceCtrl);

非常感谢您的帮助。

【问题讨论】:

  • 你试过放置“event.preventDefault();”在“skipValidation.openModal();”之后?
  • 还是同样的问题

标签: javascript angularjs angular-ui-bootstrap


【解决方案1】:

我终于找到了问题所在。 $locationChangeStart 被调用了两次,因此打开了两个模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2018-09-18
    相关资源
    最近更新 更多