Javascript Angularjs 中的 ng-touchstart 和 ng-touchend

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26170029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:26:46  来源:igfitidea点击:

ng-touchstart and ng-touchend in Angularjs

javascriptangularjsmousedownmouseuptouchstart

提问by ciembor

I have an element which fires functions on ng-mousedownand ng-mouseup. However, It doesn't work on touch screen, is there any directive like ng-touchstartand ng-touchend?

我有一个在ng-mousedown和上触发函数的元素ng-mouseup。但是,它在触摸屏上不起作用,是否有类似ng-touchstart和的指令ng-touchend

回答by Kostya Shkryob

There is a module for this: https://docs.angularjs.org/api/ngTouch

有一个模块:https: //docs.angularjs.org/api/ngTouch

But you can write your own directives for events too:

但是您也可以为事件编写自己的指令:

<!doctype html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.js"></script>
    </head>
    <body ng-app="plunker">
        <div  ng-controller="MainCtrl">
            <div my-touchstart="touchStart()" my-touchend="touchEnd()">
                <span data-ng-hide="touched">Touch Me ;)</span>
                <span data-ng-show="touched">M-m-m</span>
            </div>
        </div>
        <script>
            var app = angular.module('plunker', []);
            app.controller('MainCtrl', ['$scope', function($scope) {
                $scope.touched = false;

                $scope.touchStart = function() {
                    $scope.touched = true;
                }

                $scope.touchEnd = function() {
                    $scope.touched = false;
                }
            }]).directive('myTouchstart', [function() {
                return function(scope, element, attr) {

                    element.on('touchstart', function(event) {
                        scope.$apply(function() { 
                            scope.$eval(attr.myTouchstart); 
                        });
                    });
                };
            }]).directive('myTouchend', [function() {
                return function(scope, element, attr) {

                    element.on('touchend', function(event) {
                        scope.$apply(function() { 
                            scope.$eval(attr.myTouchend); 
                        });
                    });
                };
            }]);
        </script>
    </body>
</html>

回答by Mark Topper

I have made those ealier today since I needed it myself:

因为我自己需要它,所以我今天使它们变得更简单:

Hope it helps.

希望能帮助到你。

回答by Kevin MacDonald

The version we came up with uses $parse(), which is what $eval() uses internally. We specifically wanted to handle mousedown and touchstart events using a single directive, but in the angular way so we can include angular style expressions.

我们提出的版本使用 $parse(),这是 $eval() 内部使用的。我们特别想使用单个指令处理 mousedown 和 touchstart 事件,但以角度方式,以便我们可以包含角度样式表达式。

Like so:

像这样:

angular.module("ngStudentselect", []).directive('ngStudentselect', ['$parse', '$timeout', '$rootElement',
function($parse, $timeout, $rootElement) {
    return function(scope, element, attr) {
        var clickHandler = $parse(attr.ngStudentselect);

        element.on('mousedown touchstart', function(event) {
            scope.$apply(function() {
                clickHandler(scope, {$event: event});
            });
        });
    };
}]);

This is a stripped down version of angular's ngClick directive.

这是 angular 的 ngClick 指令的精简版。