简体   繁体   English

Bootstrap下拉菜单在Bootstrap模式下不起作用

[英]Bootstrap dropdown not working in bootstrap modal

I want to use bootstrap dropdown in a form which will be open in boostrap modal. 我想以将在boostrap模式下打开的形式使用bootstrap下拉列表。

<div class="modal fade box-sizing" id="createPerson">
<form  enctype="multipart/form-data" method="post" name="personCreateForm" novalidate>
   <div class="modal-header box-sizing">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
        <span class="lead"><h4>{{$parent.data.operation}}</h4></span>
   </div>

   <div class="modal-body box-sizing">
        <div class="form-horizontal">
            <div class="control-group">     
                <label class="control-label">Name the branch:</label>
                <div class="controls">
                    <input name="branchName" type="text" placeholder="Name the branch" id="textField"
                            ng-model="$parent.data.branchName" autocomplete="off">
                    <span class="dropdown" dropdown on-toggle="toggled(open)">
                        <a href class="title dropdown-toggle ng-binding" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <i class="fa fa-chevron-down"></i>
                        </a>
                        <ul class="dropdown-menu" style="left: inherit !important;">
                            <li>                    
                                <a ng-click="changeBranchName()">{{$parent.data.branchListData[0]}}</a>
                            </li>
                        </ul>
                    </span>
                </div>
            </div>
        </div>
    </div>  
    <div class="modal-footer box-sizing">
        <button type="button" ng-click=""               
            ng-class="{'tbt-btn':true, 'primary-btn':true}">Save</button>
        <button type="button" data-dismiss="modal" aria-hidden="true" class="btn tbt-btn secondary-btn" ng-click="close()">
             Cancel
        </button>
    </div>
</form>
</div>

javascript -- javascript-

 $('#createPerson').modal({
                show: true,
                keyboard: false,
                backdrop: 'static'
            });

but , it shows me empty dropdown even if I have values in '$parent.data.branchListData'.Sometimes (rarely) it works . 但是,即使我在'$ parent.data.branchListData'中有值,它也会显示为空的下拉列表。有时(很少)它有效。 If I copy paste same code in parent html directly without modal , it works. 如果我直接将相同代码复制粘贴到父html中而不使用modal,那么它将起作用。 Plunker 柱塞

Easy solution: By default a modals scope is the $rootScope . 简单的解决方案:默认情况下,模式范围是$rootScope when you create your modal you must set your scope property to the current scope by doing the following: 创建模态时,必须通过执行以下操作将您的scope属性设置为当前范围:

$scope.openModal = function(){
    var modalInstance = $uibModal.open({
        templateUrl: "xxxx",
        controller: "modalController",
        scope: $scope
    });
};

This also causes that you don't need parent 这也导致您不需要父母

But usually you dont want that your modal has access to ALL of the parents data. 但是通常您不希望您的模态访问所有父数据。 then You'll need to pass your data to the modal when you open your modal, using the resolve property. 那么当您打开模态时,需要使用resolve属性将数据传递给模态。 for example 例如

$scope.openModal = function(){
    var modalInstance = $uibModal.open({
        templateUrl: "xxxx",
        controller: "modalController",
        resolve: {
            myData: function(){
                return $scope.myData;
            }
        }
    });
};

And create a modalController, where you inject your resolved data. 并创建一个modalController,在其中注入已解析的数据。 Like this: 像这样:

angular.module("app.controllers").controller("modalController"["$modalInstance","$scope", "myData", modalController]);
function modalController($modalInstance,$scope, myData){
   $scope.myData = myData;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM