How to prevent an angular-bootstrap dropdown from closing (Unbind Event which was bound by a directive)

后端 未结 9 1520
你的背包
你的背包 2020-12-28 12:52

I am using the Angular-Bootstrap Dropdown. I want to prevent it from closing on click until the user closes it intentionally.

Default state is: The Dropdown closes

相关标签:
9条回答
  • 2020-12-28 13:08

    Here is what the code looks like using the approved angular-bootstrap auto-close method. Notice the auto-close attribute goes on the top <div>.

    <div class="btn-group" uib-dropdown auto-close="disabled">
        <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
            Button dropdown <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
            <textarea class="form-control" ng-model="description" rows="4" placeholder="Description"></textarea>
        </ul>
    </div>
    
    0 讨论(0)
  • 2020-12-28 13:10

    You can stop event from bubbling up in DOM Tree in angular 2 and above by adding event propagation. Ex: (click)="$event.stopPropagation()"

    0 讨论(0)
  • 2020-12-28 13:15

    This is another hack, but you could add a directive to stop the toggle event from propagating. For example something like this worked for my specific use case:

    <div>
    <div class="btn-group" dropdown is-open="status.isopen" ng-controller="DropDownCtrl">
      <button type="button" class="btn btn-primary dropdown-toggle" ng-disabled="disabled">
        Button dropdown <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" role="menu">
        <li ng-click="goToPage('Action')">Action</li>
        <li disable-auto-close>Don't Dismiss</li>
        <li ng-click="goToPage('SomethingElse')">Something else here</li>
      </ul>
    </div>
    

    Adding this directive to an element should disable the auto close behavior:

    angular.module('plunker', ['ui.bootstrap'])
    .controller('DropDownCtrl', ['$scope', '$location',
    function($scope, $location) {
      // Controller logic here
      $scope.goToPage = function(page) {
        console.log("Going to " + page + ". Dropdown should close");
        $location.path(page);
      };
    }])
    .directive('disableAutoClose', function() {
      // directive for disabling the default
      // close on 'click' behavior
      return {
            link: function($scope, $element) {
                $element.on('click', function($event) {
                    console.log("Dropdown should not close");
                    $event.stopPropagation();
                });
            }
        };
    });
    

    Plunker Example Here

    0 讨论(0)
提交回复
热议问题