单击按钮后关闭模态

时间:2016-05-24 05:56:34

标签: javascript angularjs node.js bootstrap-modal

我希望在点击“是”按钮后关闭模态但现在只运行ng-click =“”函数。单击“是”按钮后,如何关闭模态?

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Confirmation!</h4>
      </div>
      <div class="modal-body">
        Do you want to send SMS?
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
        <button type="button" class="btn btn-primary" ng-click="smsAll()">Yes</button>
      </div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:4)

在你的smsAll方法中尝试这段代码:

$('#myModal').modal('hide');

此事件由bootstrap提供,用于手动隐藏模态。

答案 1 :(得分:2)

我创建了一个示例代码。希望这会帮助你。

 $('#myModal').modal();
 var app = angular.module('myApp', []);
 app.controller('ModalCtrl', function($scope) {

   $scope.smsAll = function() {
     alert('do your stuff');
     $('#myModal').modal('hide')
   }
 });
<!DOCTYPE html>
<html lang="en">

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

</head>

<body>

  <div class="container-fluid" ng-app="myApp">

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Confirmation!</h4>
          </div>
          <div class="modal-body">
            Do you want to send SMS?
          </div>
          <div class="modal-footer" ng-controller="ModalCtrl">
            <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
            <button type="button" class="btn btn-primary" ng-click="smsAll()">Yes</button>
          </div>
        </div>
      </div>
    </div>

  </div>



</body>

</html>

答案 2 :(得分:0)

试试此代码

我添加了新属性 [data-modal-action =“close”] ..根据点击事件,它会自动关闭模态

<强> HTML

 <button type="button" class="btn btn-primary" data-modal-action="close" ng-click="smsAll()">Yes</button>

<强>脚本

$("[data-modal-action=close]").click(function () {
        $("#myModal").modal("hide");
    });