How can I close a dropdown on click outside?

I would like to close my login menu dropdown when the user click anywhere outside of that dropdown, and I’d like to do that with Angular2 and with the Angular2 “approach”…

I have implemented a solution, but I really do not feel confident with it. I think there must be an easiest way to achieve the same result, so if you have any ideas … let’s discuss šŸ™‚ !

Here is my implementation:

The dropdown component:

This is the component for my dropdown:

  • Every time this component it set to visible, (For example: when the user click on a button to display it) it subscribe to a “global” rxjs subject userMenu stored within the SubjectsService.
  • And every time it is hidden, it unsubscribe to this subject.
  • Every click anywhere within the template of this component trigger the onClick() method, which just stop event bubbling to the top (and the application component)

Here is the code

export class UserMenuComponent {

    _isVisible: boolean = false;
    _subscriptions: Subscription<any> = null;

    constructor(public subjects: SubjectsService) {

    onClick(event) {

    set isVisible(v) {
        if( v ){
            setTimeout( () => {
this._subscriptions =  this.subjects.userMenu.subscribe((e) => {
                       this.isVisible = false;
            }, 0);
        } else {
        this._isVisible = v;

    get isVisible() {
        return this._isVisible;

The application component:

On the other hand, there is the application component (which is a parent of the dropdown component):

  • This component catch every click event and emit on the same rxjs Subject (userMenu)

Here is the code:

export class AppComponent {

    constructor( public subjects: SubjectsService) {
        document.addEventListener('click', () => this.onClick());
    onClick( ) {{});

What bother me:

  1. I do not feel really comfortable with the idea of having a global Subject that act as the connector between those components.
  2. The setTimeout: This is needed because here is what happen otherwise if the user click on the button that show the dropdown:
    • The user click on the button (which is not a part of the dropdown component) to show the dropdown.
    • The dropdown is displayed and it immediately subscribe to the userMenu subject.
    • The click event bubble up to the app component and gets caught
    • The application component emit an event on the userMenu subject
    • The dropdown component catch this action on userMenu and hide the dropdown.
    • At the end the dropdown is never displayed.

This set timeout delay the subscription to the end of the current JavaScript code turn which solve the problem, but in a very in elegant way in my opinion.

If you know cleaner, better, smarter, faster or stronger solutions, please let me know šŸ™‚ !

1,899 thoughts on “How can I close a dropdown on click outside?”

  1. [url=]buy asthma inhalers without an rx[/url] [url=]kamagra 100mg oral jelly for sale[/url] [url=]levitra 20 mg dosage[/url] [url=]lowest price for viagra 100mg[/url] viagra pills buy

  2. [url=]cost of levitra 20 mg[/url] [url=]otc medications online[/url] [url=]viagra pills[/url] [url=]online viagra prescription[/url] generico de viagra

  3. [url=]viagra online prescription uk[/url] [url=]where can i buy viagra without a prescription[/url] [url=]purchase disulfiram[/url] [url=]us tadalafil[/url] [url=]finasteride for hair loss[/url] [url=]buy cialis uk paypal[/url] [url=]pfizer viagra for sale[/url] [url=]cheap cialis generic[/url] [url=]where to buy ivermectin cream[/url] [url=]20mg cialis cost[/url]

  4. How can I close a dropdown on click outside? – JavaScript Help ivermectina posologia [url=]ivermectin for humans[/url] buy ivermectin online

  5. Hey outstanding blog! Does running a blog such as this take
    a lot of work? I’ve very little understanding of computer programming however I was hoping to start my
    own blog soon. Anyway, if you have any suggestions or techniques
    for new blog owners please share. I understand this is off
    subject nevertheless I just wanted to ask. Appreciate it!

  6. Nice blog here! Also your site loads up very fast! What host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  7. Hello There. I found your blog using msn. This is an extremely well
    written article. I will make sure to bookmark it and come back to read more of
    your useful information. Thanks for the post. I’ll definitely

  8. Simply desire to say your article is as surprising. The clarity on your post is just excellent and that
    i could think you’re a professional in this subject.

    Well with your permission allow me to snatch your RSS feed to stay
    up to date with coming near near post. Thank you 1,000,000 and please keep up the rewarding work.

  9. Iā€™m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your site to come back
    down the road. All the best

  10. [url=]cheap viagra australia fast delivery[/url] [url=]malegra dxt without prescription[/url] [url=]sildenafil soft gel[/url] [url=]reputable canadian pharmacy[/url] [url=]modafinil online pharmacy usa[/url] [url=]yasmin green[/url] [url=]usa viagra over the counter[/url] [url=]buy viagra online discount[/url]

  11. Do you mind if I quote a few of your posts as long as I provide credit and sources back
    to your blog? My website is in the exact same area of interest as yours and
    my visitors would genuinely benefit from some of the information you
    provide here. Please let me know if this okay with you.
    Regards! ps4 ps4 games

  12. Pingback: viagra otc
  13. Pingback: viagra sale
  14. [url=]sildenafil otc[/url] [url=]ivermectin coronavirus[/url] [url=]tadalafil india online[/url] [url=]medication zovirax cream[/url] [url=]online pharmacy non prescription drugs[/url] [url=]cialis in canada cost[/url] [url=]canadian generic viagra 100mg[/url]

  15. Pingback: viagra alternative
  16. [url=]tadacip 10 mg price[/url] [url=]can you buy viagra over the counter in europe[/url] [url=]motilium purchase online[/url] [url=]buy generic viagra canadian pharmacy[/url] [url=]sildenafil online australia[/url] [url=]best canadian online pharmacy[/url] [url=]generic ivermectin[/url]

  17. Pingback: free cialis
  18. Pingback: viagra generic
  19. [url=]generic viagra cialis[/url] [url=]buy viagra usa online[/url] [url=]how to order viagra[/url] [url=]buy colchicine india[/url]

  20. Pingback: order viagra us
  21. Pingback: cialis logo
  22. Pingback: cheap viagra
  23. Pingback: cialis generic
  24. Pingback: viagra for sale us
  25. Pingback: cheap viagra 25