1

Hi I am developing SPA using angularjs . I have one dropdown. If the user logs in then dropdown will be clickable and it expands various options like profile,logout etc. In app.js i have below code to expand dropdown

  if (cookieloginid != null) {
            var id = document.getElementById('ProfileDropdown');
    id.innerHTML = $scope.ProfileDropdown = '<div data-drop-down>' + ' <ul>' +
        '<li><a href="#/userProfile">User Profile</a></li>' +
        '<li>test1</li>' +
       ' <li onclick="logout(event)">logout</li>' +
    '</ul>'+'</div>'; 
        }

Above code works fine. If i click on dropdown that will expand and not close. After clicking on dropdown if i click anywhere on web page i want to close the expanded dropdown. May i know is this possible? Any help would be appreciated. Thank you.

3
  • did you try: angular.element('#ProfileDropdown').triggerHandler('Click') on page load Commented Jul 14, 2017 at 10:10
  • You can use event.stopPropagation(); though it is not recommended Commented Jul 14, 2017 at 10:12
  • Thank you...... I am developing single page application. Where should i put above set of code? Commented Jul 14, 2017 at 11:27

1 Answer 1

1

you can do this by creating a custom directory for the above, see below link for the information: http://www.competa.com/blog/an-angular-click-anywhere-to-close-dropdown-menu-directive/

or else you can add your custom event on document using jquery $(document) to perform the action.

See below links: http://embed.plnkr.co/rpOgQC/

also you might want to use event.stopPropagation();, in case when you click on dropdown you might not end up in case where your drop down not getting opened.

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you. I can see dropDown directive in the plunker. So should i add one?
all i have confusion is i am using ui-routing. I have lot of ui-views. Where should i write directives?
make it global so, that you can reuse it.
Thanks. In my case no need to include clickCapture directive right?
No. you don't need that. It just for your understanding
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.