V
V
Vitaly Stolyarov2016-10-15 20:24:10
typescript
Vitaly Stolyarov, 2016-10-15 20:24:10

Accessing this from callback?

c
class PopOverTemplate {

    elem: JQuery;

    private callback(el: JQuery) {
        console.log(this);
        this.elem = el;

    }

    constructor(path: string) {

        Utils.loadTemplate(path, this.callback);
    }
};

this returns window, since the callback itself in loadTemplate is called directly from under $.get(), in pure JS this is solved by a declaration like
var _this = this;
But in TS you cannot declare variables like this. How to solve the problem in the end?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2016-10-15
@Ni55aN

Utils.loadTemplate(path, el => this.callback(el))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question