AngularJS: ngTouch 300ms Delay

前端 未结 3 1127
有刺的猬
有刺的猬 2020-12-23 00:03

This Plunkr has 2 links. The one on the left side is using the ng-click directive with the on angular-touch module inserted. As said in the angular touch module description

3条回答
  •  抹茶落季
    2020-12-23 00:51

    I've solved this by writing my own directive that listens for both touchstart and mousedown events (or touchend / mouseup, etc.). To de-dupe, I set a flag when a touch event happens, and if the flag is set I ignored all mouse events (since touch events happen before mouse events, not de-duping would result in double fires on mobile devices).

    appControllers.controller('AppCtrl', ['$scope',
     function($scope) {
      $scope._usingTouch = false;
      /* app code */
    }]).directive('lkClick', [function() {
      return function(scope, element, attr) {
        var fn = function() {
          scope.$apply(function() { 
              scope.$eval(attr.lkClick); 
            });
        }
    
        element.on('touchstart', function(event) {
            scope._usingTouch = true;
            lk();
        });
    
        element.on('mousedown', function(event) {
          if(!scope._usingTouch)
            lk();
        });
      };
    }]);
    

    Then you can add the lk-click="javascript()" directive in your app's html.

    Using Fastclick is easier and quicker, but this is more customizable and doesn't require you to load Fastclick code.

提交回复
热议问题