$event.stopPropagation()
Say you've created a piece of HTML that has a couple of divs to be clicked on:<div data-ng-click="topClick()">
<h1>Click me!</h1>
<div data-ng-click="bottomClick()">
<h2>Click me too!</h2>
</div>
</div>
With the above code, every time you click the div with the h2 and trigger bottomClick(), you're also going to trigger topClick(). This may cause your code to execute when you don't want it to, and is most likely going to lead to a really confusing experience (and an unexpected journey) for your user.
The solution here is to add $event.stopPropagation() into the ngClick along with bottomClick(), so that your code looks like:
<div data-ng-click="bottomClick(); $event.stopPropagation()">
It doesn't matter if it goes before or after the function call you want to trigger, but I think it makes more sense and is easier to read if you place it afterwards.
I've hosted this example if you want to see it in action.
Note: I've used
data-ng-repeat
here so that my html passes validation. You can choose whether or not to prefix ng-repeat - and what with -
based on your own needs.
No comments:
Post a Comment