I have a button inside of an accordion, like this:
I made the entire accordion head clickable by doing this:
<accordion-heading ng-click="isopen=!isopen">
<div>
<span class="accordion-header">
<i class="pull-right glyphicon"
ng-class="{'glyphicon-chevron-down': accordionOpen,
'glyphicon-chevron-right': !accordionOpen}"></i>
</span>
<button class="delete-button" ng-click="remove()">Delete</button>
</div>
</accordion-heading>
The problem I have is when I click the delete button, remove() is called AND the accordion open/closes.
Is there a way to stop the accordion header from opening/closing when I click the delete button?
You can use $event.preventDefault(); $event.stopPropagation();
:
<button class="delete-button" ng-click="
$event.preventDefault(); $event.stopPropagation(); remove()">Delete</button>
The click event object is accessible in ng-click as $event
. preventDefault and stopPropagation will stop the event from reaching the accordion-heading click handler.
You need to stop the event from bubbling to the container
$scope.remove = function(event) {
event.preventDefault();
event.stopPropagation();
...
}
and
<button class="delete-button" ng-click="remove($event)">Delete</button>
Demo: plnkr
For me, if I use both functions
$event.preventDefault();
$event.stopPropagation();
nothing happened at all. So I use only
$event.stopPropagation();
It works good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With