Bootstrap模态框作为jQuery对象

5
使用 Bootstrap 模态框的示例:
<div class="modal fade">
  <div class="modal-dialog">
    <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">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

我在想是否有一种方法可以通过 jQuery 使这个东西变得动态一些。
我的目标是创建一个名为 $theModal 的变量,该变量将被初始化,并具有用于获取/设置标题、内容、单击保存/取消/关闭时要调用的 JavaScript 函数等属性。
应该全部通过 jQuery 生成,还是应该在代码中使用标识符/自定义数据属性来捕获模态框?
也许可以使用某种类结构?
var $theModal = new MyModal();

下一个问题是,如果模态框已经打开,如何创建一个克隆? 我会尝试/猜测。
var $theClone = $theModal.clone().init();

$theClone.title = "Title of the second modal";
$theClone.content = $.ajax(url);
$theClone.saveAction = saveTheContentOfTheContent;
$theClone.show();

这是否可能,或者我的假设完全错误?
2个回答

0
你的想法还不错,但是...
你可以在你的HTML代码中只有一个模态框。然后,在你标题所在的位置,你可以使用$scope类型。
接着,在你的js文件中,你可以将你想要的字符串指定为数组,只需在ng-click函数的参数中更改为你想要的那个就好了。

index.html

<p>This is the modal view.</p>

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>

  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">{{msgArray.title}}</h4>
        </div>
        <div class="modal-body">
          <p>{{msgArray.body}}</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">{{msgArray.btn}}</button>
        </div>
      </div>

    </div>
  </div>

script.js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
      $scope.msgArray1 = {
        title : "Modal 1",
        body: "This is the co to say anything you want here.",
        btn : "Close",
    };
     $scope.msgArray2 = {
        title : "Modal 2",
        body: "This is the co to say anything you want here.",
        btn : "Close",
    };

        $scope.msgTemp = {
        title : "",
        body: "",
        btn : "",
    };

    $scope.openModal = function(modal){

      if(modal == "modal1"){
        $scope.msgTemp = $scope.msgArray1;
      }
              if(modal == "modal2"){
        $scope.msgTemp = $scope.msgArray2;
      }

      $("#myModal").modal('show');

    }

});

http://plnkr.co/edit/nV3iq1U1ymcsBAXT6g2y?p=preview


0

你的想法非常好,而且已经被某人实现了。请查看这个链接

这个链接展示了你的想法可以应用于每种可能的情况。它还有一些我们甚至没有想到的巨大特点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接