Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
25
rated 0 times [  28] [ 3]  / answers: 1 / hits: 15770  / 12 Years ago, sat, november 17, 2012, 12:00:00

I have this typescript code:



    module MyPage {

export class MyVm {

ToDo : string;

Load() {
//can access todo here by using this:
this.ToDo = test;

$.get(GetUrl, function (servertodos) {
//but how do I get to Todo here??
this.ToDo(servertodos); //WRONG ToDo..
});
}
}
}


The question is, how do I access the todo member field in the $.get callback?


More From » typescript

 Answers
4

TypeScript also supports arrow function that preserve lexical scoping. Arrow functions result in similar code to Jakub's example but are neater as you don't need to create the variable and adjust usage yourself:



Here is the example using an arrow function:



$.get(GetUrl, (todos) => {
this.ToDo(todos);
});

[#81944] Thursday, November 15, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anayaashleyh

Total Points: 597
Total Questions: 96
Total Answers: 86

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
anayaashleyh questions
;