Giter Club home page Giter Club logo

angularjs-dropzone's Introduction

File Upload using Dropzone.js and angularJS

##Introduction

The whole angularJS code exists in app/assets/javascripts/app.js

We are creating a directive named drop-zone and a controller to test this directive named UploadCtrl.

Usage

Where uploadURL is the url to uplaod the files (coming from the controller) and done is the callback when the file upload is finished successfully.

Directive:

.directive('dropZone', function() {
  return {
    restrict: 'A',
    scope: {
      upload: '@',
      done: '&'
    },
    template : '<div id="upload-dropzone" class="upload-box" > </div>',
    replace: true,
    link: function(scope, element) {
      var optionsObj = {
        maxFilesize: 1024
      };

      if(scope.done) {
        optionsObj.success = function(file, response) {
          scope.$apply(function() {
            scope.done({file: file, response: response});
          });
        };
      }

      scope.$watch('upload', function(oldValue, newValue) {
        optionsObj.url = newValue;
        element.dropzone(optionsObj);
      });
    }
  };
})

We will replace the directive with the template, thus we have the replace flag to true. This is just to keep the DOM small.

In the link function we are creating a option object for the dropzone check the http://www.dropzonejs.com/#configure url for full options. Keep in mind that if we are configuring events (like the success event) then we need to inform angularJS that the event has changed value, for that we need to call the scope.$apply method.

We want to add the upload url programmatically. When we are creating the dropzone it will take the url from either the action or the options and configure the dropzone with this value. However at the link phase angular hasn't evaluated the upload variable yet so if we just call the element.dropzone at that point we will have as the url the string { { uploadURL } }. To solve this we need to create the dropzone once the upload variable has been evaluated, so we need to call a $watch function on 'upload' variable and create the dropzone once the url is set up.

angularjs-dropzone's People

Contributors

makisarvin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.