0

I need some help. I build the following directve to translate docx file into html string.

(function(){
'use strict';
 angular
   .module('app.core')
       .directive('uploadFile', uploadFile);

        function uploadFile($rootScope, $parse){
        var directive = {

                    restrict: "A",
                    scope:{result : '='},
                    controller: 'refertazioneController',
                    controllerAs: "vm",
                    link: linkFunction,


        };

                function linkFunction(scope, element, attrs, controller){

                    document.getElementById("document")
                        .addEventListener("change", handleFileSelect, false);

                    function handleFileSelect(event) {
                        readFileInputEventAsArrayBuffer(event, function(arrayBuffer) {
                            mammoth.convertToHtml({arrayBuffer: arrayBuffer})
                                .then(displayResult)
                                .done();
                        });
                    }

                    function displayResult(result) {
                            scope.vm.result = resutl.value;

                /*        document.getElementById("output").innerHTML = result.value;

                        var messageHtml = result.messages.map(function(message) {
                            return '<li class="' + message.type + '">' + escapeHtml(message.message) + "</li>";
                        }).join("");

                        document.getElementById("messages").innerHTML = "<ul>" + messageHtml + "</ul>";*/

                    }

                    function readFileInputEventAsArrayBuffer(event, callback) {
                        var file = event.target.files[0];

                        var reader = new FileReader();

                        reader.onload = function(loadEvent) {
                            var arrayBuffer = loadEvent.target.result;
                            callback(arrayBuffer);
                        };

                        reader.readAsArrayBuffer(file);
                    }

                    function escapeHtml(value) {
                        return value
                            .replace(/&/g, '&amp;')
                            .replace(/"/g, '&quot;')
                            .replace(/</g, '&lt;')
                            .replace(/>/g, '&gt;');
                    }

                                };

            return directive;
            }


})();

the problem is that i'm not able to retrivie the translate string in the controller, defined as follows:

        (function(){
                'use strict';
                angular
                    .module('app.core')
                        .controller('refertazioneController', refertazioneController);

                function refertazioneController($stateParams, refertationService, $window, examinationService, dataService, $scope){
                    var vm = this;
                    vm.prova="refertazioneController";
                    vm.tinymceModel = '';
                    vm.sospeso=true;
                    vm.datiDaRefertare = $stateParams;
                    vm.paziente = dataService.getPatient(vm.datiDaRefertare.patientId);

                    examinationService.getPatientExamsDef(vm.datiDaRefertare.patientId).then(function(r){
                        vm.subjectExam = r.data[0].data;
                    })

                    console.log(vm.paziente);
                    vm.currentUser = sessionStorage;
                    vm.tinymceOptions = {
                    onChange: function(e) {
                    // put logic here for keypress and cut/paste changes
                    },
                    inline: false,
                    slector: 'textarea',
                    // toolbar: 'undo redo | styleselect | bold italic | link image | print save cancel',
                    height: 500,
                    plugins : 'advlist autolink link image lists charmap print preview template save paste',
                    skin: 'lightgray',
                    theme : 'modern',
                    language:'it',
                    statusbar: false,
                    templates:[ {title: 'Titolo1', description: 'Descrizione1', content: '<p style="text-align: center;">'+
                                                                                        '<strong>A.S.L. 02 LANCIANO-VASTO-CHIETI</strong>'+
                                                                                        '</p>'},
                                {title: 'Titolo2', description: 'Secondo referto', url: 'sections/refertazione/referto1.html'}
                                ]
                    };

                    vm.html = {};
                    //vm.html.content = '<p>qui per esempio ci va il template che mi ridà il back end</p><h2>altra roba</h2>';

                    refertationService.openRefert(1,2);
                    refertationService.closeRefert(1,2);
                    refertationService.saveRefert(1,2);

                /*  vm.testoHtml = "";*/

                }
        })();

I thought that the line : scope.vm.result = result.value was able to bind the string to my controller and then that i was able to render it in the view as refertazione.result (refertazione is the name of my controller). But this not works, where I'm wrong?

1 Answer 1

1

A slightly better pattern that relies on events. You could pull this same pattern off with a scope variable that is two way. Idea is you use an event to tell the controller data has changed.

function uploadFile($rootScope, $parse) {
    var directive = {
        restrict: "A",
        scope: {},
        link: linkFunction,

    };

    function linkFunction(scope, element, attrs) {
        var fn = $parse(attrs.uploadFile);
        element.on('change', function(onChangeEvent) {
            var reader = new FileReader();
            console.log(reader);
            reader.onload = function(onLoadEvent) {
              $rootScope.$broadcast('fileupdate', onLoadEvent.target.result);
            };
            reader.readAsText((onChangeEvent.srcElement || onChangeEvent.target).files[0]);
        });
    }
  return directive;
}

Inside of your controller you would listen for the fileupdate event.

//inside your controller:
$scope.$on('fileupdate', showContent);
function showContent(event, $fileContent){
   vm.content = $fileContent;
}
Sign up to request clarification or add additional context in comments.

3 Comments

your solution is good but in the vm.content I can not obtain the txt content...i.e. I'm not able to render vm.content in my view (the controller name is refertazione so I would be able to render vm.content as refertazione.content). If a log(vm.content) I can read the txt content
I just followed your example, and I'm not following your issue. Are you trying to alias it to a diff vm variable? And are you sure the reader.onload code is working correctly?
I'm trying to render the txt content in the view. As I can log in the console the txt content, I think reader.onload is working correctly. I appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.