bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

ui.bootstrap.dropdownToggle ng-click doesn't close menu

Open raff77 opened this issue 13 years ago • 15 comments

When using ng-click with the dropdownToggle, Angular-Ui does not close the menu:

<div class="control-group input-append">
                <div class="btn-group" >
                    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">Quick Range <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a>Past Month</a></li> <!-- THIS WILL CLOSE THE MENU -->
                    <li><a ng-click="past3Month()">Past 3 Months</a></li> <!-- THIS WILL NOT -->
                    <li><a ng-click="past6Month()">Past 6 Months</a></li>
                    <li><a ng-click="pastYear()">Past Year</a></li>
                  </ul>
                </div>
            </div>

raff77 avatar Aug 09 '13 14:08 raff77

That's because ng-click is overriding the click event on the parent element of your links. The way dropdown works, is it opens then binds a click event to the parent of the trigger (in this case <div class="btn-group">) which closes the dropdown. ngClick takes a higher priority in this case, and prevents the default behavior (to close it).

This is by design, as dropdownToggle as currently written is really expecting links (it is designed to be a navigation tool). If you're looking to implement what you are trying to do, you'd need to rewrite dropdownToggle in an isolate scope, and define the close() function on that scope. Then an ng-click="$parent.past3Month(); close()" would do as you expect.

hallister avatar Aug 10 '13 15:08 hallister

There's an info about angular 1.2.0rc1:

ngMobile (ngClick): fix: emit click event for touchy clicks (fb7d891d, #3219, #3218, #3137) Shouldn't this help ?

rtpm avatar Aug 14 '13 08:08 rtpm

@rtpm perhaps for mobile clicks. @hall5714 yes that makes sense but as this is a port of Bootstrap I would expect an implementation in Angular to be exactly as it is in Bootstrap.

Anyway adding the $event to the handler eg. ng-click="past3Month($event)"

and then $event.target.parentElement.parentElement.parentElement.click();

fixed it but I am not happy with this and have decided to build my own drop down directive.

Thanks lads

raff77 avatar Aug 14 '13 09:08 raff77

@raff77 I wouldn't think of this so much as a port of Bootstrap, but an implementation of Bootstraps CSS, with widgets written the Angular Way. Because JQuery is a DOM manipulation library, and Angular is a full-stack Javascript framework that operates on a very different paradigm, expecting them to be exactly the same is only asking to get frustrated.

That, and Bootstrap is the most popular project on Github, and this project was primarily created by a group of 5-6 developers (if that). So building a directive that fits your use case is great! And if you decide to, you can always do a PR and share your changes with the community :+1:

hallister avatar Aug 14 '13 09:08 hallister

Wunderbar :)

raff77 avatar Aug 14 '13 09:08 raff77

I'm going to close this for now. If you end up creating the new directive and wish to contribute, please open a PR. I expect that this functionality will eventually be implemented as a custom "