使用带有Jquery.1.12.4的dataTransfer将angularJS指令迁移到Jquery.3.2.1

时间:2017-04-13 13:56:34

标签: jquery angularjs

我已经使用了我在其他帖子中找到的下一个角度拖放指令。

启用" dataTransfer"使用Jquery 1.12.4,我使用了:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

但如果我用jquery.3.2.1替换Jquery 1.12.4,我有下一个错误:

(index):104 Uncaught TypeError: Cannot read property 'push' of undefined
    at (index):104

有没有办法用Jquery.3.2.1设置dataTransfer?

// angular directive

angular
  .module('clinang')
  .directive('dragMe', dragMe)
  .directive('dropOnMe', dropOnMe);

dragMe.$inject = [];

function typeInTextarea(el, newText) {
  var start = el.selectionStart
  var end = el.selectionEnd
  var text = el.value
  var before = text.substring(0, start)
  var after  = text.substring(end, text.length)
  el.value = (before + newText + after)
  el.selectionStart = el.selectionEnd = start + newText.length
  el.focus()
}

function dragMe() {
  var DDO = {
    restrict: 'A',
    link: function(scope, element, attrs) {
      element.prop('draggable', true);
      element.on('dragstart', function(event) {
        event.dataTransfer.setData('text', event.target.id)
      });
    }
  };
  return DDO;
}
dropOnMe.$inject = [];
function dropOnMe() {
  var DDO = {
    restrict: 'A',
    link: function(scope, element, attrs) {
      element.on('dragover', function(event) {
        event.preventDefault();
      });
      element.on('drop', function(event) {
        event.preventDefault();
        var data = event.dataTransfer.getData("text");
        var x=document.getElementById(data);
        typeInTextarea(event.target,x.getAttribute('data-value'))
      });
    }
  };
  return DDO;
}

0 个答案:

没有答案