简体   繁体   中英

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

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>

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();
    ...
}

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> 

也可以使用它,因为它使用相同。

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

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;
    }
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM