Skip to content

Instantly share code, notes, and snippets.

@brianpkelley
Created March 20, 2015 15:25
Show Gist options
  • Save brianpkelley/7fdd4d911b45e857de8c to your computer and use it in GitHub Desktop.
Save brianpkelley/7fdd4d911b45e857de8c to your computer and use it in GitHub Desktop.
Angular Touch Events. Replicated angular event directives adapted for touch events.
// Direct copy + paste of angular event directives adapted for touch events.
'use strict';
var prefix = 'ng';
var tbTouch = angular.module(prefix+'TouchEvents',[]);
angular.forEach(
'touchstart touchmove touchend'.split(' '),
function(eventName) {
var directiveName = prefix+eventName[0].toUpperCase()+eventName.substr(1);
tbTouch.directive(directiveName, ['$parse', '$rootScope', function($parse, $rootScope) {
return {
restrict: 'A',
compile: function($element, attr) {
var fn = $parse(attr[directiveName], /* interceptorFn */ null, /* expensiveChecks */ true);
return function ngEventHandler(scope, element) {
element.on(eventName, function(event) {
var callback = function() {
fn(scope, {$event:event});
};
scope.$apply(callback);
});
};
}
};
}]);
}
);
// Just include the module "ngTouchEvents" (substituting your own prefix) and use it just like ng-click or other: ng-touchstart="callbackFn($event)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment