Robert
Robert

Reputation: 375

Angular-UI run dropdownToggle from ng-click

Is there any way to run dropdownToggle from ng-click directive ? I am using it with ngMobile to avoid delayed click of "Actions" button on touch devices.

  <div class="btn-group">
      <a class="btn dropdown-toggle">
        Actions
        <span class="caret"></span>
      </a>
      <ul class="dropdown-menu">
          <li><a>Action 1</a></li>
          <li><a>Action 2</a></li>
      </ul>
  </div>

There is also a problem with menu, which doesn't close on tap if I don't keep the Action1/Action2 pressed for a long time. (much longer than usual tap).

Upvotes: 1

Views: 3140

Answers (2)

thanhquanky
thanhquanky

Reputation: 123

I have a similar problem when dropdown form doesn't close on $location.path(newPath) My solution is, if you use uncompressed ui-bootstrap file, in

angular.module('ui.bootstrap.dropdownToggle', [])
    .directive('dropdownToggle', ['$document', '$location', function($document, $location)

Change

scope.$watch('$location.path', function() {
    closeMenu();
});

To

scope.$watch('$routeChangeStart', function() {
    closeMenu();
});

Upvotes: 2

Robert
Robert

Reputation: 375

Looks like I found a solution (not the best one I assume) but it works. If someone has a better one, please share.

Opening a dropdown adds an "open" class to the dropdown element, so in order to get it opened on using ng-click I needed to add this class.

I know it's a temporary solution but didn't find the better one.

Here's the code:

<div class="btn-group" ng-class="{open: opened}" ng-click="opened=!opened"  ng-init="opened=false">
      <a class="btn dropdown-toggle">
        Actions
        <span class="caret"></span>
      </a>
      <ul class="dropdown-menu">
          <li><a ng-click="$location.path('/some/addr')">Action 1</a></li>
      </ul>
  </div>

As for closing menu on click: this could be caused by error in my html (not properly closing some tag). After correction, problem disappeared. From source I see that dropDown menu is closed on both click & running $location.path()

Upvotes: 2

Related Questions