Synchronize attributes, angular component

1

I am using an "external" bower component (although I can modify it if necessary) to upload files to a server. This component when the upload completes notifies a method of my controller and establishes in the model an upload identifier. The issue is that from my controller when I have more than one element does not work.

The component when sending does:

function ctoUpload() {
    return {
        restrict: 'E',
        scope: {
            identificador: '=',
            identificadorObj: '=',
            onSuccess: '&'
        },
        controller: UploadFilesController,
        controllerAs: 'uploadController',
        templateUrl: '{{lang}}/upload.html',
        bindToController: true
    };
}


  function uploadFile() {
        var inputArchivo =document.getElementById('file' + uploadController.identificadorObj);
        var formData = new FormData();
        formData.append('file', inputArchivo.files[0]);
         var onsuccess = uploadController.onSuccess;

        uploadService.subirFichero(formData,  onsuccess).success(function (response, status) {
            if (_.isFunction(onsuccess)) {
                uploadController.identificador = response.id;//id correcto
                            result = $q.when(uploadController.onSuccess.apply(this));

result.then (function () {...

My "callback" does the following:

vm.upload = function(file) {
     var id = vm.identificador;//aquí id vale erróneamente el anterior
     ...

The jade:

cto-upload(id="opt"+"vm.identificador",identificador="vm.identificador", 
           on-success="vm.upload(file)"..

The result is that when uploading a file vm.identificador it is "" when uploading the second it takes the previous one and so on. I have tried with $ scope.apply and $ scope. $ EvalAsync without success. Debugging I see that the component gets all the data correctly but it is not updated in my model. Any ideas?

    
asked by Esther Gómez 13.03.2018 в 10:57
source

1 answer

0

Test the logic that interacts with angular components within a function driven by the angular life cycle, such as $timeout

uploadService.subirFichero(formData,  onsuccess).success(function (response, status) {
    if (_.isFunction(onsuccess)) {
        $timeout(function (){
            /* tu logica aqui*/
        });
    }
});

This is because the assignment you make:

uploadController.identificador = ...

Not visible in a cycle of digest

    
answered by 13.03.2018 в 13:13