0

I'm trying to access variables inside a typescript class. The first one checkLUCE1 is easily editable adding this.

The second one checkLUCE2 is inside another function and i don't know how to set a new value for it:

export class ServiziConfigurazioneComponent implements OnInit {

constructor(private savelocal: SavelocaleService,
          private globals: GlobalsService) { }


checkLUCE1:boolean = false;
checkLUCE2:boolean = false;

handleFileSelect(evt,servizio,i){

    let target = evt.target || evt.srcElement || evt.currentTarget;
    let idAttr = target.attributes.id;
    let value = idAttr.nodeValue;
    let obj:any;

    document.getElementById(value+'-image').style.display="none";
    document.getElementById(value+'-loading').style.display = "block";

    let files = evt.target.files;
    let file = files[0];
    this.checkLUCE1 = true; //HERE I CAN SET THE VALUE

    if (files && file) {
        let reader = new FileReader();

        reader.onload = function() {
            (<HTMLImageElement>document.getElementById(value+'-image-file')).src  = reader.result;
            let binaryString = reader.result;
            let base64textString = btoa(binaryString);
            //console.log(base64textString);

            let obj = {};
            obj['file-'+servizio+'-'+i]= {
                'file' : base64textString
            };
            checkLUCE2= true; //HERE I CAN'T SET THE VALUE

            localStorage.setItem('currentUserLuceFile-'+i,base64textString);
        };
        reader.readAsDataURL(file);


        document.getElementById(value+'-image').style.display="block";
        document.getElementById(value+'-loading').style.display = "none";

        //this.savelocal.write('currentUserOfferta',JSON.stringify(obj));
    }
  }
}
0

2 Answers 2

4

You can use arrow functions in order to achive this. So instead of declaring your function like reader.onload = function() {...} you can use reader.onload = () => {...}. Have a look at this example.

class Bla {

    // Your variables
    luce1: string = "sdfgsy";
    luce2: string = "sdfysd";

    // In your case that was "handleFileSelect"
    public doSomething() {
        alert(this.luce1);
        // this is what you are looking for. Inside the arrow function you can set the class level variable
        let anonymous = () => { this.luce2 = "Success" }

        anonymous();

        alert(this.luce2);
    }

}

let bla = new Bla();

bla.doSomething();
Sign up to request clarification or add additional context in comments.

2 Comments

@SergioGandrus You have to omit the word function! Just remove it. It has to be reader.onload = () => {...}
@SergioGandrus Great! Please accept this answer if it helped you.
0

There are three ways to solve it:

Store the context in a variable:

var self=this;

document.onload=function () {
  self.myAttribute=...
};

Use arrow functions, where the context is set in declaration time, not in execution time:

document.onload=() => {
  this.myAttribute=...
}

Bind the context to the function using bind:

document.onload=(function () {
  this.myAttribute=...
}).bind(this);

Comments

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.