javascript 获取错误 Dropzone 已经附加了角度指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24118187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:12:39  来源:igfitidea点击:

Getting error Dropzone already attached with angular Directives

javascriptangularjsdropzone.js

提问by anam

I am using following code for drop zone but i am getting error, i tried to debug it but i am not able to resolve this action plz guide

我正在使用以下代码放置放置区,但出现错误,我尝试对其进行调试,但无法解决此操作请参阅指南

http://jsfiddle.net/anam123/rL6Bh/

http://jsfiddle.net/anam123/rL6Bh/

 -------------------> "Error: Dropzone already attached.

  throw new Error("Dropzone already attached.");" 

Code::

代码::

https://gist.github.com/compact/8118670

https://gist.github.com/compact/8118670

snippts:

片段:

 /**
 * An AngularJS directive for Dropzone.js, http://www.dropzonejs.com/
 * 
 * Usage:
 * 
 * <div ng-app="app" ng-controller="SomeCtrl">
 *   <button dropzone="dropzoneConfig">
 *     Drag and drop files here or click to upload
 *   </button>
 * </div>
 */

angular.module('dropzone', []).directive('dropzone', function () {
  return function (scope, element, attrs) {
    var config, dropzone;

    config = scope[attrs.dropzone];

    // create a Dropzone for the element with the given options
    dropzone = new Dropzone(element[0], config.options);

    // bind the given event handlers
    _.each(config.eventHandlers, function (handler, event) {
      dropzone.on(event, handler);
    });
  };
});

angular.module('app', ['dropzone']);

angular.module('app').controller('SomeCtrl', function ($scope) {
  $scope.dropzoneConfig = {
    'options': { // passed into the Dropzone constructor
      'url': 'upload.php'
    },
    'eventHandlers': {
      'sending': function (file, xhr, formData) {
      },
      'success': function (file, response) {
      }
    }
  };
});

回答by anam

Solved issue by using following code setup.

通过使用以下代码设置解决了问题。

So you can either:

所以你可以:

  1. Turn off autoDiscover globally like this: Dropzone.autoDiscover = false;, or
  2. Turn off autoDiscover for specific elements like this: Dropzone.options.myAwesomeDropzone = false;
  1. 像这样全局关闭自动发现:Dropzone.autoDiscover = false;, 或
  2. 为特定元素关闭自动发现,如下所示: Dropzone.options.myAwesomeDropzone = false;

Reference:
FAQ on dropzone

参考:
dropzone 常见问题

回答by Farid Amiri

Nothing worked for me so i went to the dropzone.js file and change the line that throw an error (i think in many versions it's in line 426):

没有什么对我有用,所以我转到 dropzone.js 文件并更改引发错误的行(我认为在许多版本中它在第 426 行):

if (this.element.dropzone) {
    throw new Error("Dropzone already attached.");
  }

so i replace

所以我更换

throw new Error("Dropzone already attached.");

with

return this.element.dropzone;

and it is working

它正在工作

回答by ProgrammerCk

Dropzone.autoDiscover = false;
$('#bannerupload').dropzone({
    url: "/upload",
    maxFilesize: 100,
    paramName: "file",
    maxThumbnailFilesize: 5,
    init: function() {      
      this.on('success', function(file, json) {       
        jQuery("input#mediaid").val(json);
      });
    }
  });

回答by Atul

I was facing same issue "Dropzone already attached" because we enabled myDropzoneobject in the script and were trying to enable again.

我遇到了同样的问题“Dropzone 已经附加”,因为我们myDropzone在脚本中启用了对象并试图再次启用。

For example

例如

if ($('#upl').attr('class')) {

var myDropzone = new Dropzone("#upl", {init: function () {

and again trying to enable it

并再次尝试启用它

 if (jQuery('#password').attr('save_profile')) {
  var myDropzone = new Dropzone("#upl", {init: function () {

with another action.

用另一个动作。

Please check your code.

请检查您的代码。

回答by Eric Corriel

Put your create dropzone code inside of a try/catch block

将创建 dropzone 代码放在 try/catch 块中

try{
 $('.dropzone').dropzone({
    url: '/upload'
  });
}
catch(error){
    console.log("Catching " + error);
}