How to update a variable in controller (ControllerAs syntax) from directive in Angularjs?

82 views Asked by At

I have a dropzone directive:

function dropzone() {
    return {
        restrict: 'C',
        link: function(scope, element, attrs) {

            var config = {
                url: 'http://192.168.1.114:8080/api/files',
                maxFilesize: 100,
                paramName: "file",
                maxThumbnailFilesize: 10,
                parallelUploads: 1,
                autoProcessQueue: true
            };

            var eventHandlers = {
                 'success': function (file, response) {
                }

            };

            dropzone = new Dropzone(element[0], config);

            angular.forEach(eventHandlers, function(handler, event) {
                dropzone.on(event, handler);
            });
        }
    }
}

Where the response in success event handlers is the uploaded file id.

I'd like to keep a list of ids of all uploaded files in my uploadFileFormCtrl:

function uploadFileFormCtrl() {
    var _this = this;

    _this.fileIds = [];
};

This will work for a specific controller, i.e. uploadFileFormCtrl as uploadFile

                'success': function (file, response) {
                    scope.uploadFile.fileIds.push(Number(response));
                    scope.$apply();
                }

However I have multiple different controllers (for example: uploadFile, uploadImage, etc) which have the dropzone file uploader, so I can't add a specific controller name in the directive.

How should the directive and the controller communicate with each other to achieve this?

0

There are 0 answers