在chrome中的event.dataTransfer.setData(mimeType,angular.toJson(item))之后没有设置HTML5 DragDrop dataTransfer.types

时间:2017-04-13 11:51:28

标签: drag-and-drop dom-events custom-events angulardraganddroplists

我正在使用druska / native_js_drag_and_drop_helper.js自动angular-drag-and-drop-lists

问题:  即使在event.dataTransfer.setData(mimeType,angular.toJson(item))

之后

dataTransfer.types未设置或可能不允许在dragstart和drop事件中读取

观察:在手动拖放dropstart事件期间

dataTransfer {dropEffect: "none", effectAllowed: "move", items: DataTransferItemList, types: Array(1), files: FileList}
    dropEffect:"move"
    effectAllowed:"move"
    files:FileList
    items:DataTransferItemList
    types:Array(0)
    __proto__:DataTransfer

DataTransferItemList {0: DataTransferItem, length: 1}
    length: 0
    __proto__: DataTransferItemList

 DataTransferItem {kind: "string", type: "application/x-dnd"}
    kind: ""type: ""
    __proto__: DataTransferItem

观察:使用自定义事件时

dataTransfer:Object
    data:Object
        application/x-dnd:"[{"id":"706","title":"Ab 2","drag":false,"selected":true}]"
        __proto__:Object
    dropEffect:"move"
    effectAllowed:"move"
    getData:function (type)
    setData:function (type,val)
    files:Object
    items:Object
    types:Array(0)

我正在使用

var event = new CustomEvent("CustomEvent", {"cancelable": true})
        event.initCustomEvent(type, true, true, null);
        event.dataTransfer = {
            data: {
            },
            setData: function(type,val) {
                this.data[type] = val
            },
            getData: function(type) {
                return this.data[type]
            },
            dropEffect: 'move',
            effectAllowed:'move',
            types: [],
            items:{},
            files:{}


        }

我无法初始化dataTransfer对象。  dataTransfer.items和dataTransfer.types是只读属性,我不知道如何正确  初始化它们。MDN dataTransfer

1 个答案:

答案 0 :(得分:0)

问题是setData dint有代码来设置在拖放期间由angular-drag-and-drop-lists使用的DataTransfer.types。

        setData: function(type,val) {
            this.data[type] = val
            this.types[0] = type
        }

以上更改解决了问题。使用原生js

,拖放工作正常