Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable button inside of a clickable panel

I have a button inside of an accordion, like this:

enter image description here

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?

like image 482
Jason Avatar asked Aug 21 '14 06:08

Jason


3 Answers

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.

like image 166
joakimbl Avatar answered Nov 12 '22 18:11

joakimbl


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

like image 43
ivarni Avatar answered Nov 12 '22 16:11

ivarni


For me, if I use both functions

$event.preventDefault();
$event.stopPropagation();

nothing happened at all. So I use only

 $event.stopPropagation();

It works good.

like image 41
Olga Akhmetova Avatar answered Nov 12 '22 17:11

Olga Akhmetova