Angularjs中的ng-touchstart和ng-touchend

时间:2014-10-02 21:35:31

标签: javascript angularjs mousedown mouseup touchstart

我有一个元素可以激活ng-mousedownng-mouseup上的函数。但是,它在触摸屏上不起作用,是否有ng-touchstartng-touchend这样的指令?

2 个答案:

答案 0 :(得分:14)

有一个模块: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>
&#13;
&#13;
&#13;

答案 1 :(得分:8)

我已经在今天早些时候做了那些,因为我自己需要它:

希望它有所帮助。