Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fastClick + AngularJS for iPad

Tags:

angularjs

ipad

There is a delay when clicking with ng-click on iPad with AngularJS I have the generate directive I need to write

my_app.directive 'touch', ->
  (scope, e, attr) ->
    e.fastClick (e) ->
      scope.$apply attr.fastClick

But it doesn't know what fastClick is even though I've included it in my application. I would think it needs to be created as a service and then injected into my directive, but how?

like image 822
Andrew WC Brown Avatar asked Nov 30 '22 13:11

Andrew WC Brown


2 Answers

Just in case someone else finds this, who does not use coffee script, here is the conversion

 app.directive("ngTap", function() {
  return function($scope, $element, $attributes) {
    var tapped;
    tapped = false;
    $element.bind("click", function() {
      if (!tapped) {
        return $scope.$apply($attributes["ngTap"]);
      }
    });
    $element.bind("touchstart", function(event) {
      return tapped = true;
    });
    $element.bind("touchmove", function(event) {
      tapped = false;
      return event.stopImmediatePropagation();
    });
    return $element.bind("touchend", function() {
      if (tapped) {
        return $scope.$apply($attributes["ngTap"]);
      }
    });
  };
});

it's gfTop, becuase the sample is "good films" app. Feel free to change that to whatever you like.

Also note, that you have to change all your "ng-click"s to "gfTap"s.

UPDATED: to handle tap and click events.

like image 178
nycynik Avatar answered Dec 06 '22 23:12

nycynik


It's pretty simple to implement your own tapping directive without an external library. I would advise that.

Goodfilms went over it in their blog post about their AngularJS mobile app: http://goodfil.ms/blog/posts/2012/08/13/angularjs-and-the-goodfilms-mobile-site-part-1/

Here's their tap code (also in Coffeescript), straight from the blog post:

app.directive 'gfTap', ->
  (scope, element, attrs) ->
    tapping = false
    element.bind 'touchstart', -> tapping = true
    element.bind 'touchmove', -> tapping = false
    element.bind 'touchend', -> scope.$apply(attrs['gfTap']) if tapping
like image 26
Andrew Joslin Avatar answered Dec 07 '22 00:12

Andrew Joslin