angular-multi-select
angular-multi-select copied to clipboard
Another buttons click event fires before on-close
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
Did you find solution? Because i'm also experiencing this problem
My current workaround is to fire the on-close event manually during the search button's click event
Working example: http://embed.plnkr.co/YOlzjtEXOseugxkyonWS/
@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.
- The way
externalClickEvent
(responsible for callingonClose
event) listener has been registered ondocument
, it callsonClose
event on it. For your case, the way event propagation works is it callsbutton
event first, and then callexternalClickEvent
(registered ondocument
) -
$timeout
wrapped aroundonClose
method, will eventually queued uponClose
method and call it on next digest afterng-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();
Note: Not sure about what would be implication of this solution.