Reputation: 1432
I use this directive to render Dropzone.js
in page:
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
angular.forEach(config.eventHandlers, function (handler, event) {
dropzone.on(event, handler);
});
};
});
and in my controller use this code:
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) {
}
}
};
});
In Dropzone to show files already stored on server use mockFile
and this.emit
for this. Now how to get this
and run emit
function?
Upvotes: 4
Views: 2514
Reputation: 1432
I solved problem with this:
'use strict';
angular.module('dropzone', []).directive('dropzone', function ($timeout) {
return {
restrict:'AE',
require: 'ngModel',
link:function (scope, element, attrs, ngModel) {
var init = function () {
var config, dropzone;
config = scope[attrs.dropzone];
// create a Dropzone for the element with the given options
dropzone = new Dropzone(element[0], config.options);
// Display existing files on server
if(ngModel.$viewValue !=='' && ngModel.$viewValue !==undefined){
var mockFile = {name: ngModel.$viewValue, size: 1234};
dropzone.emit("addedfile", mockFile);
dropzone.createThumbnailFromUrl(mockFile, "uploads/" + ngModel.$viewValue);
dropzone.emit("complete", mockFile);
}
// Form submit rest dropzone event handler
scope.$on('dropzone.removeallfile', function() {
dropzone.removeAllFiles();
});
// bind the given event handlers
angular.forEach(config.eventHandlers, function (handler, event) {
dropzone.on(event, handler);
});
};
$timeout(init, 0);
}
}
});
and in controller:
$scope.dropzoneConfig = {
options: { // passed into the Dropzone constructor
url: '/api/uploadimage',
paramName: "file", // The name that will be used to transfer the file
maxFilesize: .5, // MB
acceptedFiles: 'image/jpeg,image/png,image/gif',
maxFiles: 1,
},
'eventHandlers': {
'removedfile': function (file,response) {
$http({
method : "POST",
url : "/api/uploadimage/"+$scope.customer.avatar_url
}).then(function mySucces(response) {
$scope.deleteMessage = response.data;
$scope.customer.avatar_url='';
});
},
'success': function (file, response) {
$scope.customer.avatar_url = response.filename;
}
}
};
and in your Html:
<div ng-if="customer.avatar_url!==undefined" ng-model="customer.avatar_url" dropzone="dropzoneConfig" class="dropzone"></div>
Upvotes: 3