4

I have a html code like the following:

<div class="outerdiv" data-ng-click="resetText()">
    <div class="innerdiv" data-ng-click="showText()">
        {{ text }}
    </div>
</div>

An outer div with a ng-click and an inner div with a different ng-click.

My problem is: when I click the inner div, the outer one is being fired as well. What can I do to solve this (fire the inner function, not the outer one?

I can make it work using a hardcoded flag, but not sure if I'm in face of a race condition problem.

Here is a Fiddle illustrating the problem.

Beterraba
  • 6,515
  • 1
  • 26
  • 33
  • possible duplicate of [What's the best way to cancel event propagation between nested ng-click calls?](http://stackoverflow.com/questions/15193539/whats-the-best-way-to-cancel-event-propagation-between-nested-ng-click-calls) – Stewie Feb 11 '14 at 12:58

2 Answers2

11
<div class="innerdiv" data-ng-click="showText($event)">

and in the controller

$scope.showText = function(event) {
    // whatever
    event.stopPropagation();
}
doodeec
  • 2,927
  • 19
  • 23
3

Try this

<div class="outerdiv" data-ng-click="resetText()">
    <div class="innerdiv" data-ng-click="showText();$event.stopPropagation()">
        {{ text }}
    </div>
</div>
Jay Shukla
  • 5,794
  • 8
  • 33
  • 63