UPDATE
Looked at allenhwkim's answer and indeed he is correct, that $event
's propagation can be easily stopped. I took it for granted (without checking), that attaching ng-swipe-*
directive to other element will start firing separate events. I was clearly wrong.
HERE is updated fiddle.
The below answer is basically rubbish.
There is still one problem with stopPropagation
-- the mouse up event seems not to fire.
The most elegant solution would be to decorate the ng-swipe-*
directives or $swipe
service -- but looking into their code and I do not think it is possible.
Other option would be to create your own directive, that would manually attach ng-swipe-*
, take care of the compilation process and provide desired functionality. Surely, bit complicated.
What I came up with is a quick hack. The idea is to add an attribute to an element whose descendants should not fire the ng-swipe-*
.
js
myApp.value('shouldFire', function(element){
var update = true;
// strange enough $event.fromElement is always null
var current = element;
while(current && current != document.body){
if(current.getAttribute('swipe')=='cancel'){
update = false;
break;
}
current = current.parentElement;
}
return update;
})
function MyCtrl($scope, shouldFire) {
$scope.sidebar = false;
$scope.updateSidebar = function($event, show){
var element = $event.toElement || $event.srcElement;
shouldFire(element) && ($scope.sidebar = show);
}
}
html
<div class="cont" ng-swipe-right="updateSidebar($event, true)"
ng-swipe-left="updateSidebar($event, false)">
...
<div class="pan-container" panhandler=""
content-width="500px" swipe="cancel">
UPDATED DEMO
caution
- in android's chrome.v.39 angular-panhandler is broken.
$event
stores a proper touch event (android's chrome) or a custom event (desktop's chrome); in the latter case $event.fromElement
is always null
.
- The proposed solution is a quick hack -- it is neither elegant nor general. Nonetheless, theoretically it could be possible to support multi
ng-swipe-*
handlers by setting different values in swipe
attribute.