Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Both outer and inner divs have ng-click and when I click on the inner div, both ng-clicks execute. How to prevent that?

I have 2 divs, an outer and inner one that have ng-click attached to both of them. When I try to click the inner div, both functions execute ( I assume because I’m essentially also clicking the outer div because it contains the inner one ). How can I prevent the outer div’s function from executing when clicking on the inner div?

<div ng-click="vm.showMoreHandler()">
    <div ng-click="vm.actionHandler($event, action)"></div>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use event.stopPropagation() to keep it from bubbling up

angular.module('myApp', [])
  .controller('myCtrl', ['$scope', function($scope) {
    $scope.showMoreHandler = function(e) {
      console.log('showMoreHandler');
    }
    $scope.actionHandler = function(e) {
      e.stopPropagation();
      console.log('actionHandler');
    }

  }]);
div {
  padding: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-click="showMoreHandler($event)">showMoreHandler
    <div ng-click="actionHandler($event)">actionHandler</div>
  </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading