Sunday, June 16, 2019
angularjs - What's the best way to cancel event propagation between nested ng-click calls?
Answer
Answer
Here's an example. Let's say I want to have an image overlay like a lot of sites. So when you click a thumbnail, a black overlay appears over your whole window, and a larger version of the image is centered in it. Clicking the black overlay dismisses it; clicking the image will call a function that shows the next image.
The html:
The javascript:
function OverlayCtrl($scope) {
$scope.hideOverlay = function() {
// Some code to hdie the overlay
}
$scope.nextImage = function() {
// Some code to find and display the next image
}
}
The problem is that with this setup, if you click the image, both nextImage()
and hideOverlay()
are called. But what I want is for only nextImage()
to be called.
I know you can capture and cancel the event in the nextImage()
function like this:
if (window.event) {
window.event.stopPropagation();
}
...But I want to know if there's a better AngularJS way of doing it that won't require me to prefix all of the functions on elements inside the overlay with this snippet.
Answer
What @JosephSilber said, or pass the $event object into ng-click
callback and stop the propagation inside of it:
$scope.nextImage = function($event) {
$event.stopPropagation();
// Some code to find and display the next image
}
plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV
In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...
-
This question attempts to collect the few pearls among the dozens of bad C++ books that are published every year. Unlike many other programm...
-
I need to do the following: My current address looks like: https://www.domain.com I want to redirect with htaccess: www.domain.com TO https:...
-
using namespace std; So far in my computer science courses, this is all we have been told to do. Not only that, but it's all tha...
No comments:
Post a Comment