引导模式只在第二次调用时显示


Bootstrap modal displays only on second call

当我第一次调用这个函数时,什么都没有发生,但在第二次点击时,它工作得很好。后续单击时,显示前一次单击的结果。下面是我的代码

 <table class="table">
        <thead>
        <tr class="text-center" style="font-weight: bold">
            <td></td>
            <td>Name</td>
            <td>ID</td>
            <td>Age</td>
            <td>Registered On</td>
            <td>View Prescription</td>
        </tr>
        </thead>
        <tr ng-repeat="x in patientList | filter:filter_patient_list" class="text-center">
            <td>{{x.list}}</td>
            <td>{{x.name}}</td>
            <td>{{x.patient_id}}</td>
            <td>{{x.age}}</td>
            <td>{{x.date_registered}}</td>
            <td><button data-toggle="modal"  data-target="#view_prescription_from_patient_list{{x.patient_id}}" ng-click="getPrescriptionModal(x.patient_id)">/button></td>
        </tr>
    </table>

下面是我的angularJs代码

$scope.getPrescriptionModal = function(patient_id)
{
    $http.get("ajax_get_data.php?get_what=prescription_list_for_patient_list&patient_id="+patient_id).success(function(response2)
    {
        $('#view_prescription_from_patient_list'+patient_id).modal('show');
        $('.modal_show').append(response2);
    }).error(function()
    {
        alert("Unable to fetch");
    })
}

和我的PHP代码获得模态窗口

    if($get_what == "prescription_list_for_patient_list")
{
    $patient_id = $_GET['patient_id'];
echo"
<div class='modal fade' id='view_prescription_from_patient_list$patient_id' 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'>Modal title</h4>
      </div>
      <div class='modal-body'>
        ...
      </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>
  </div>
</div>
";

我试着用Jquery调用Ajax,也有同样的问题。

似乎你正在试图打开一个模态之前,它存在于DOM。

尝试先将响应附加到<body>,以便存在用于搜索它的选择器。

如果您正在使用的ID仍然只在响应html中,使用$('#id')搜索现有的DOM元素,因此不会找到它

$http.get(url, function(response){
    $('body').append(response);
    $(selector).modal();
});

或者你也可以尝试;

$(response).modal();

我不确定bootstrap是否会自动将响应附加到DOM或不这样做。

还强烈建议摆脱bootstrap.js,并使用angular-ui-bootstrap指令。