ng-touchstart and ng-touchend in Angularjs

前端 未结 3 2023
名媛妹妹
名媛妹妹 2021-01-04 01:41

I have an element which fires functions on ng-mousedown and ng-mouseup. However, It doesn\'t work on touch screen, is there any directive like

3条回答
  •  無奈伤痛
    2021-01-04 01:52

    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.

    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.

提交回复
热议问题