简体   繁体   中英

Is there an AngularJS directive to use ngClick, which only fires up on touch events?

Is it possible to call a method only when the input was initiated through a touch screen? eg on the Surface Studio you have two possible input devices - mouse and touch screen.

I want to react differently based on the type of input device used. As far as I know, feature detection won't work as it only tells me if a device is capable of smth. But not if a specific action was called via touch screen.

I scribbled the following directive.

It's not even close to good but it perfectly fits my needs for now.

angular.module('app').directive('tap', function ($parse) {
    return {
        restrict: 'A',
        compile: function($element, attr) {    
            return function (scope, element) {    
                element.on('touchstart', function(event) {    
                    var callback = function() {
                        $parse(attr['tap'])(scope, {$event: event});
                    };    
                    scope.$apply(callback);    
                });    
            };    
        }
    };
});

Usage:

<a tap="mainCtrl.someTouchscreenAction()">Tap</a>

Maybe someone else will find it useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM