How to prevent parent click event when the user clicks on his child element? AngularJS

本小妞迷上赌 提交于 2020-06-25 07:40:09

问题


I have a <div> with a ng-click but this <div> have a child element with also a ng-click directive. The problem is that the click event on the child element trigger also the click event of the parent element.

How can I prevent the parent click event when I click on his child?

Here is a jsfiddle to illustrate my situation.

Thank you in advance for your help.

EDIT

Here is my code:

<body ng-app ng-controller="TestController">
<div id="parent" ng-click="parentClick()">
    <div id="child" ng-click="childClick()"></div>

    <p ng-bind="elem"></p>
</div>

<div><p style="text-align:center" ng-bind="childElem"></p></div>
</body>

<script>
function TestController($scope) {
    $scope.parentClick = function() {
        $scope.elem = 'Parent';
    }

    var i = 1;
    $scope.childClick = function() {
        $scope.elem = 'Child';
        $scope.childElem = 'Child event triggered x' + i;
        i++;
    }
}
</script>

回答1:


You should use the event.stopPropagation() method. see: http://jsfiddle.net/qu86oxzc/3/

<div id="child" ng-click="childClick($event)"></div>

$scope.childClick = function($event) {
    $event.stopPropagation();
    ...
}



回答2:


Use event.stopPropagation to stop the event from bubbling up the DOM tree from child event handler.

Updated Demo

function TestController($scope) {
  $scope.parentClick = function() {

    $scope.elem = 'Parent';
  }

  var i = 1;
  $scope.childClick = function(e) {
    $scope.elem = 'Child';
    $scope.childElem = 'Child event triggered x' + i;
    i++;

    e.stopPropagation(); // Stop event from bubbling up
  }
}
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}
#parent {
  width: 100px;
  height: 100px;
  position: relative;
  z-index: 1;
  margin: 10px auto 0 auto;
  background-color: #00acee;
  border-radius: 2px;
  cursor: pointer;
}
#parent:hover {
  opacity: 0.5;
}
#child {
  width: 20px;
  height: 20px;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  background-color: #FFF;
  border-radius: 2px;
  cursor: pointer;
}
#child:hover {
  opacity: 0.5;
}
#parent p {
  width: 100%;
  position: absolute;
  bottom: 0px;
  text-align: center;
  color: #FFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>

<body ng-app ng-controller="TestController">
  <div id="parent" ng-click="parentClick()">
    <div id="child" ng-click="childClick($event)"></div>
    <!--                                 ^^^^^^^     -->
    <p ng-bind="elem"></p>
  </div>
  <div>
    <p style="text-align:center" ng-bind="childElem"></p>
  </div>
</body>



回答3:


also you can use this as it use do the same.

<div id="child" ng-click="childClick();$event.stopPropagation();"></div>



回答4:


Even if Pieter Willaert's answer is much more beautiful i updated your fiddle with a simple boolean check:

http://jsfiddle.net/qu86oxzc/6/

function TestController($scope) {
    $scope.boolean = false;
    $scope.parentClick = function () {
        if (!$scope.boolean) $scope.elem = 'Parent';
        $scope.toggleBoolean();
    }

    var i = 1;
    $scope.childClick = function () {
        $scope.boolean = true;
        $scope.elem = 'Child';
        $scope.childElem = 'Child event triggered x' + i;
        i++;
    }

    $scope.toggleBoolean = function () {
        $scope.boolean = !$scope.boolean;
    }
}



回答5:


You can also try $event.stopPropagation();. write it after the child function. It working like a preventdefault.

<body ng-app ng-controller="TestController">
<div id="parent" ng-click="parentClick()">
    <div id="child" ng-click="childClick();$event.stopPropagation();"></div>

    <p ng-bind="elem"></p>
</div>

<div><p style="text-align:center" ng-bind="childElem"></p></div>
</body>


来源:https://stackoverflow.com/questions/33281148/how-to-prevent-parent-click-event-when-the-user-clicks-on-his-child-element-ang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!