在AngularJS中如何在开始触摸/拖动时调用一个方法

How to call a method on starting touch/drag in AngularJS

本文关键字:调用 方法 一个 拖动 AngularJS 触摸 开始      更新时间:2023-09-26

我在AngularJS中使用https://github.com/fatlinesofcode/ngDraggable进行拖放。它工作得很好,但是当用户开始拖动时,我需要做一些操作。这是myMethod()。我实现了一个ng-mousedown。在台式机上没有问题,但在触屏设备上无法运行。

 <div ng-drag="true" ng-drag-data="myObject" ng-mousedown="myMethod()">
    123
 </div>

任何想法?

对于所有有同样问题的人:

我使用了ngTouch模块的$swipe服务。给定的div现在是一个指令。拖拽时myMethod()被调用。

HTML:

<my-directive ng-drag="true" ng-drag-data="myObject" on-drag="myMethod()"></my-directive>

指令:

angular.module('myApp')
.directive('myDirective', ['$swipe',
  function($swipe) {
    return {
      restrict: 'EA',
      scope: {
        onDrag: '&'
      },
      link: function(scope, ele, attrs, ctrl) {
      $swipe.bind(ele, {
          'start': function(coords) {
            scope.onDrag();
          }
      }
    }
}]);