Skip to content

Instantly share code, notes, and snippets.

@mhuneke
Created November 6, 2012 18:02
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mhuneke/4026406 to your computer and use it in GitHub Desktop.
Save mhuneke/4026406 to your computer and use it in GitHub Desktop.
Angular ng-tap directive
(function(angular) {
'use strict';
var directives = angular.module('app.directives', []);
directives.directive('ngTap', function() {
return function(scope, element, attrs) {
var tapping;
tapping = false;
element.bind('touchstart', function(e) {
element.addClass('active');
tapping = true;
});
element.bind('touchmove', function(e) {
element.removeClass('active');
tapping = false;
});
element.bind('touchend', function(e) {
element.removeClass('active');
if (tapping) {
scope.$apply(attrs['ngTap'], element);
}
});
};
});
})(angular);
@Urigo
Copy link

Urigo commented Feb 13, 2013

Why is ng-tap not apart of angularJS library?
Can we add it there?

@gnrlbzik
Copy link

@Urigo good question.

@dmackerman
Copy link

I think better mobile support is coming in Angular 1.2. Including gestures, etc.

This is a great solution for now, however!

@obiddle
Copy link

obiddle commented Aug 28, 2014

Captain Obvious: this doesn't work when testing on a browser, make sure to use a mobile device

@brianpkelley
Copy link

You can test in Chrome, Developer Console -> (press escape) -> Emulation -> Sensors -> Emulate Touch Screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment