1x2x3x4x
1x2x3x4x

Reputation: 604

Public variable won't update in typescript

I have a class HomePage {} and inside it I have a plubic variable like this:

public resultado: any;

Also I have two methods and a function set up ike this, inside the same class.

l1() {
    this.l2();
}

l2() {
    l3();
    function l3(){
        this.resultado = "TEST";
    }
}

And it won't change the variable to TEST, like I don't have access to it even if public.

Upvotes: 0

Views: 46

Answers (1)

Ciarán Tobin
Ciarán Tobin

Reputation: 7536

What error are you getting? You should probably be getting

TypeError: Cannot set property 'resultado' of undefined

... because this in l3() is referring to the function itself not the class. You need an arrow function or bind().

l2() {
    const l3 = () => {
        this.resultado = "TEST";
    };

    l3();
}

Upvotes: 3

Related Questions