16

と で関数を起動する要素がありng-mousedownますng-mouseup。ただし、タッチスクリーンでは動作しません。ng-touchstartやのようなディレクティブはありng-touchendますか?

4

3 に答える 3

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>

于 2014-10-03T09:51:54.347 に答える
8

私はそれを自分で必要としていたので、今日それらをより早く作成しました:

それが役に立てば幸い。

于 2015-02-03T13:23:32.587 に答える