如何禁用父ng-click?

时间:2015-09-26 16:52:34

标签: angularjs

我有ng-click的HTML结构:

<div ng-click="parent()">
  <div ng-click="d"></div>
</div>

如果我执行ng-click="parent()"

,我如何禁用外ng-click="d"

2 个答案:

答案 0 :(得分:29)

在角度stopPropagation对象上使用$event方法:

<div ng-click="parent()">
  <div ng-click="d(); $event.stopPropagation();"></div>
</div>

或者将$event对象作为ng-click方法的参数传递,并在方法中调用stopPropagation

<div ng-click="parent()">
  <div ng-click="d($event)"></div>
</div>

d

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

答案 1 :(得分:2)

仅添加event.stopPropagation();在孩子的ng点击。它将阻止来自父ng-click 例如:

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