简体   繁体   English

是否有使用ngClick的AngularJS指令,该指令仅在触摸事件时触发?

[英]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. 例如,在Surface Studio上,您有两个可能的输入设备-鼠标和触摸屏。

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. 也许其他人会发现它有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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