Muhad
Muhad

Reputation: 283

How to disable parent ng-click?

I have HTML structure with ng-click:

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

How I can disable outside ng-click="parent()" if I do ng-click="d"

Upvotes: 15

Views: 9247

Answers (2)

Tabish
Tabish

Reputation: 1722

Just add $event.stopPropagation(); on the child ng-click. It will prevent the parent ng-click.

Example:

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

Upvotes: 2

Michel
Michel

Reputation: 28359

Use the stopPropagation method on the angular $event object:

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

Or pass the $event object as an argument of the ng-click method, and call stopPropagation in the method:

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

In d:

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

Upvotes: 39

Related Questions