ui.bootstrap.dropdownToggle ng-click doesn't close menu
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>
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.
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 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 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:
Wunderbar :)
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 "
Hello @raff, did you get around to writing a new dropdown directive? I'm having exactly the same issue as described here.
Sorry @perqa, was away for a while. No I added a hack. I have since upgraded to Bootstrap 3 and it works fine.
Just upgraded to 0.11.0 and got the same problem here.. Looks like i have to add a ugly hack :(
You can use the is-open attribute to manage this without any ugly hack. In your ng-click method just set the model for is-open to false.
What is really bugging me is that
<li><span ng-click="something()">I close the menu</span></li>
closes the menu as expected when clicked. This doesn't:
<li><a ng-click="something()">I doesn't close the menu</a></li>
Is there any way we can make <a> behave like <span> in this regard?
@abrander It's angular stuff
I've used x-name attribute for workaround.
<li><a x-name="option" ng-click="something()">I close the menu</a></li>
The dropdown-toggle directive on a link works to close (toggle) the dropdown when the link is clicked.
<li><a dropdown-toggle ng-click="something()">I close the menu</a></li>
Thank you compact, this solution works for me :)
Added support for multiple dropdown-toggle directives so @compact solution now works again on the latest version. https://github.com/plarsson/bootstrap/commit/644dc5222634477b1251a86b709106f888535cb5