Created
March 20, 2015 15:25
-
-
Save brianpkelley/7fdd4d911b45e857de8c to your computer and use it in GitHub Desktop.
Angular Touch Events. Replicated angular event directives adapted for touch events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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