0

I have a few functions in my .ts file, to have an access from a function to other functions and to global variables, I do: var self = this, and then self. - and a I have an access to all my fucntions and variables.
But:
Having a problem with knockout binding in following example:
<span data-bind="text: CatNom", click: $parent.ClickedItem /> and in my
public ClickedItem(obj, event) { var self = this } - and here, this is my obj in parameter.
How to declare this as a instance of my class to have an access to all my properties in my entire class.
Thank you in advance!

Alex McManns
  • 257
  • 1
  • 4
  • 13

2 Answers2

1

Found a solution. Used lambda expression.
So I have:

public ClickedItem = (obj, event) => { var self = this }

Instead of

public ClickedItem(obj, event) { var self = this }

TypeScript and Knockout binding to 'this' issue - lambda function needed?

Community
  • 1
  • 1
Alex McManns
  • 257
  • 1
  • 4
  • 13
1

Just use an arrow function and you don't need to even create a self variable:

clickedItem = (obj, event) => { 
    console.log(this) // `this` is the class instance
}

More : https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

basarat
  • 261,912
  • 58
  • 460
  • 511