Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another buttons click event fires before on-close #548

Open
brentvee opened this issue Jan 23, 2018 · 4 comments
Open

Another buttons click event fires before on-close #548

brentvee opened this issue Jan 23, 2018 · 4 comments

Comments

@brentvee
Copy link

I'm experiencing a weird issue where I have a search button with a bound ng-click="search()".
When opening the multi-select control, selecting some items, then immediately clicking my search button (so just clicking away from the multi-select, but directly on my search button), the search button's click event is firing before the on-close event of the multi-select.
Is there something I should be doing differently?
Thanks

@th0mas86
Copy link

Did you find solution? Because i'm also experiencing this problem

@brentvee
Copy link
Author

My current workaround is to fire the on-close event manually during the search button's click event

@th0mas86
Copy link

Working example: http://embed.plnkr.co/YOlzjtEXOseugxkyonWS/

@pankajparkar
Copy link
Contributor

pankajparkar commented Jun 16, 2018

@brentvee In my understanding that happens because it is using $timeout to fire the event, which eventually queued up the event and fire that event in next digest cycle. Check code here. Find detailed explanation and solution below.

Root cause of this could be because of two things.

  1. The way externalClickEvent(responsible for calling onClose event) listener has been registered on document, it calls onClose event on it. For your case, the way event propagation works is it calls button event first, and then call externalClickEvent(registered on document)
  2. $timeout wrapped around onClose method, will eventually queued up onClose method and call it on next digest after ng-click method got fire.

To fix the issue you can change the event bubbling order, to fire document event first and then the button event by replacing this line by below line

// true flat change the direction of event propagation / bubbling
document.addEventListener('click', $scope.externalClickListener, true); 

and change $timeout with $apply calling on immediate next line

$timeout( function() {
   $scope.onClose();
}, 0 );

to

 $scope.onClose();
 $scope.$apply();

Working Plunker

Note: Not sure about what would be implication of this solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants