Skip to content

Instantly share code, notes, and snippets.

@mklickman
Forked from anonymous/controller.js
Last active October 19, 2016 16:41
Show Gist options
  • Save mklickman/4dbf624bed2a5434d1c63dcad1bb427a to your computer and use it in GitHub Desktop.
Save mklickman/4dbf624bed2a5434d1c63dcad1bb427a to your computer and use it in GitHub Desktop.
<div class="app-root" ng-controller="rootController">
<div class="carousel">
<!--
I have a UI Bootstrap carousel with some slides in it. Each slide
needs to be both clickable and swipeable, with a mouse (this problem
is not an issue on mobile).
The problem I'm having is that when you swipe with a mouse click,
letting the mouse up at the end of the swipe will trigger the click
(which opens a modal). I need the click to only be called if no swipe
happens. That is what the swipe handler directive is there for, but I
need to trigger the parent scope's click handler from the directive's
link function.
How do I do that?
-->
<div class="slide"
swipe-handler-directive
click-handler="clickHandler(slide, $index)"
ng-repeat="slide in slides track by $index"
></div>
</div><!-- .carousel -->
</div><!-- .app-root -->
angular.module('myApp')
.controller('rootController', function($scope) {
$scope.clickHandler = function(slide, $index) {
// do something with 'item' and $index
};
})
;
angular.module('myApp')
.directive('swipe-handler-directive', function() {
return {
restrict: "A",
scope: {
clickHandler: '&'
},
link: function($scope, $element, $attrs) {
$element.on('click', function() {
$scope.clickHandler(slide, $index);
}
}
}
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment