Angularjs中的ng-touchstart和ng-touchend

cie*_*bor 16 javascript mouseup mousedown touchstart angularjs

我有上触发功能的元件ng-mousedownng-mouseup.但是,它在触摸屏上不起作用,是否有任何指令ng-touchstartng-touchend

Kos*_*yob 13

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

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

<!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>
Run Code Online (Sandbox Code Playgroud)

  • Angular的ngTouch库不支持绑定到单个触摸和释放事件:http://stackoverflow.com/questions/19985097/can-angulars-ngtouch-library-be-used-to-detect-a-long-click-touch-保持 - 释放 (3认同)

Mar*_*per 8

因为我自己需要,所以我今天做得更早了:

希望能帮助到你.