【问题标题】:Drag and Drop using AngularJS (with or without jQuery), how?使用 AngularJS(有或没有 jQuery)拖放,如何?
【发布时间】:2014-03-26 22:56:27
【问题描述】:

我想要做的正是 this ,但那是在 jQuery 中,我想知道是否有办法在 AngularJS 中做到这一点,或者是否有人已经用 Angular 的方式做到了,如果没有,它是如何假设 Angular 允许您自定义指令来解决此类问题的。

我知道要使用 jQuery,但我想继续使用 AngularJS,它非常令人困惑,所以如果我的问题不够好,我很抱歉,但我发现的只是 this,而这并不一点帮助都没有。我真的很感谢您对此提出的任何建议。

【问题讨论】:

  • 嘿只是说,你不必强迫它,而且不要使用 jQuery。我认为 AngularJs 可以毫无问题地与 jQuery 一起使用!例如对于这样的事情
  • @GonzaloNaveira 好的,谢谢,但可以说我想使用那些可拖动的 jQuery 元素作为指令的模板,只需按照video 中的说明进行操作就可以正常工作吗?跨度>
  • 例如,如果你声明你的指令 那么在 jQuery 中你可以访问它 $('someting')。这对你有帮助吗?
  • 好吧,如果它有效,那么是的,但我怀疑这是否有效。 (我只是在使用 AngularJS 迈出第一步)。
  • 不使用jQuery拖放:directiv.es/Angular-DragDrop

标签: jquery angularjs


【解决方案1】:

我是这样做的,它有点复杂,因为我包括添加事件处理程序(开始、拖动、停止)和容器元素的能力。这是演示JSFiddle Without jQuery 的工作小提琴。这是另一个使用 jQuery 和 jQueryUI [JSFiddle w/jQuery] 的版本。希望能帮助到你。 JSFiddle With jQuery and jQueryUI.

你可以这样使用它

ng-draggable='dragOptions'

你的控制器在哪里

$scope.dragOptions = {
    start: function(e) {
      console.log("STARTING");
    },
    drag: function(e) {
      console.log("DRAGGING");
    },
    stop: function(e) {
      console.log("STOPPING");
    },
    container: 'container-id'
}

这是指令。

.directive('ngDraggable', function($document) {
  return {
    restrict: 'A',
    scope: {
      dragOptions: '=ngDraggable'
    },
    link: function(scope, elem, attr) {
      var startX, startY, x = 0, y = 0,
          start, stop, drag, container;

      var width  = elem[0].offsetWidth,
          height = elem[0].offsetHeight;

      // Obtain drag options
      if (scope.dragOptions) {
        start  = scope.dragOptions.start;
        drag   = scope.dragOptions.drag;
        stop   = scope.dragOptions.stop;
        var id = scope.dragOptions.container;
        container = document.getElementById(id).getBoundingClientRect();
      }

      // Bind mousedown event
      elem.on('mousedown', function(e) {
        e.preventDefault();
        startX = e.clientX - elem[0].offsetLeft;
        startY = e.clientY - elem[0].offsetTop;
        $document.on('mousemove', mousemove);
        $document.on('mouseup', mouseup);
        if (start) start(e);
      });

      // Handle drag event
      function mousemove(e) {
        y = e.clientY - startY;
        x = e.clientX - startX;
        setPosition();
        if (drag) drag(e);
      }

      // Unbind drag events
      function mouseup(e) {
        $document.unbind('mousemove', mousemove);
        $document.unbind('mouseup', mouseup);
        if (stop) stop(e);
      }

      // Move element, within container if provided
      function setPosition() {
        if (container) {
          if (x < container.left) {
            x = container.left;
          } else if (x > container.right - width) {
            x = container.right - width;
          }
          if (y < container.top) {
            y = container.top;
          } else if (y > container.bottom - height) {
            y = container.bottom - height;
          }
        }

        elem.css({
          top: y + 'px',
          left:  x + 'px'
        });
      }
    }
  }

})

【讨论】:

  • 谢谢,您能否添加一个链接,其中记录了如何以这种方式学习自定义指令? (可能是您学习它的那个或那些)即使不是,对您的回答也很有帮助。
  • @jCuga,我为你添加了一个小提琴。如果您有任何问题,请告诉我。
  • @VdeVatman,我会这么认为。您可以添加说 elem.on('mousedown touchstart')、bind('mousemove touchmove') 和 bind('mouseup touchend'),然后解除绑定。然后确保在您的应用程序中包含 angular-touch.js 模块。
  • 我很想看到调整元素大小的选项 :)
  • @ZackArgyle 感谢分享。是否有可能将 x 和 y 轴保存在范围内?比如如果我想在屏幕上显示新位置的 x 和 y 轴怎么办?抱歉,对 Angular 很陌生。给你更多的权力,并提前感谢。 :)
【解决方案2】:

很久以前,我已经围绕 jQueryUI 可拖放/可拖放创建了一个包装器。可能对你有帮助。

演示:http://codef0rmer.github.com/angular-dragdrop/#/

来源:https://github.com/codef0rmer/angular-dragdrop

【讨论】:

  • 是的,我在 google 中搜索过,并且 yoy 是 3 个第一个 google 回复,但这并不是我想要的。但是我有你的 page 最喜欢的,因为我看到它对学习如何一起使用 AngularJS 和 jQuery,你似乎提供了关于如何创建指令的非常有用的信息。
【解决方案3】:

使用 AngularJS 进行拖放的最佳示例。设计简单,步骤简单。

首先定义anguler应用程序(ng-app)名称,定义两个指令和一个控制器,如下所述。还应用一些 css 来改善 html 的外观和感觉

只需运行 sn-p 并享受编码。

var module = angular.module('my-app', []);

module.directive('draggable', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      element[0].addEventListener('dragstart', scope.handleDragStart, false);
      element[0].addEventListener('dragend', scope.handleDragEnd, false);
    }
  }
});

module.directive('droppable', function () {
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      element[0].addEventListener('drop', scope.handleDrop, false);
      element[0].addEventListener('dragover', scope.handleDragOver, false);
    }
  }
});

function MainController($scope)
{
    $scope.drag_types = [
        {name: "Charan"},
        {name: "Vijay"},
        {name: "Mahesh"},
      {name: "Dhananjay"},
    ];
    $scope.items = [];
    
    $scope.handleDragStart = function(e){
        this.style.opacity = '0.4';
        e.dataTransfer.setData('text/plain', this.innerHTML);
    };
    
    $scope.handleDragEnd = function(e){
        this.style.opacity = '1.0';
    };
    
    $scope.handleDrop = function(e){
        e.preventDefault();
        e.stopPropagation();
        var dataText = e.dataTransfer.getData('text/plain');
        $scope.$apply(function() {
            $scope.items.push(dataText);
        });
        console.log($scope.items);
    };
    
    $scope.handleDragOver = function (e) {
        e.preventDefault(); // Necessary. Allows us to drop.
        e.dataTransfer.dropEffect = 'move';  // See the section on the DataTransfer object.
        return false;
  };
           
}
.container {
  width: 600px;
  border: 1px solid #CCC;
  box-shadow: 0 1px 5px #CCC;
  border-radius: 5px;
  font-family: verdana;
  margin: 25px auto;
}

.container header {
  background: #f1f1f1;
  background-image: -webkit-linear-gradient( top, #f1f1f1, #CCC );
  background-image: -ms-linear-gradient( top, #f1f1f1, #CCC );
  background-image: -moz-linear-gradient( top, #f1f1f1, #CCC );
  background-image: -o-linear-gradient( top, #f1f1f1, #CCC );
  box-shadow: 0 1px 2px #888;
  padding: 10px;
}

.container h1 {
  padding: 0;
  margin: 0;
  font-size: 16px;
  font-weight: normal;
  text-shadow: 0 1px 2px white;
  color: #888;
  text-align: center;
}

.container section {
  padding: 10px 30px; 
  font-size: 12px;
  line-height: 175%;
  color: #333;
}
<script src="http://code.angularjs.org/1.2.13/angular.js"></script>
<div ng-app="my-app" ng-controller="MainController">
    <div class="container">
        <header><h1>Drag students from here</h1></header>
        <section>
            <div draggable="true" ng-repeat="drag_type in drag_types">{{drag_type.name}}</div>
        </section>
    </div>
    <div class="container">
        <header><h1>Drop students here</h1></header>
        <section droppable="true">
            <div><span>You dragged in: </span>
                <span ng-repeat="name in items track by $index">
                    <span ng-show="$index != 0">,</span>
                    {{name}}
                </span>
            </div>
        </section>
    </div>
    
     See the JSON: <pre>{{items|json}}</pre>
</div>

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2011-05-28
    • 2010-11-22
    相关资源
    最近更新 更多