0

Under my Angular app : i ve this template :

<dxi-column dataField="ordre"
              caption="Ordre"
              [width]="70"
              dataType="number"
              [allowEditing]="true">
     <dxi-validation-rule type="async"
              [validationCallback]="myFunction"
              message="">
     </dxi-validation-rule>
</dxi-column>

and myFuntion looks like this :

  myFunction=(params) => {
    console.log(this.myClassVariable)
    return Of(!this.myClassVariable.includes(params.value)).toPromise()
  }

My purpose is to change "myFunction" declaration to be something like this :

myFunction() { 
  //SAME TREATMENT
}

Maybe also change the invocation in the html , but basically keep the same beahviour , i ve tried this :

myFunction() { 
    console.log(this.myClassVariable)  // THROW UNDEFINED
    return Of(!this.myClassVariable.includes(params.value)).toPromise()    
}

but i got UNDEFINED for my class variable passing

Suggestions ??

firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
  • Search for “difference between normal function and arrow function”, perhaps. There is no relevant difference between an anonymous function and a “named” function _of the same “kind”_. – user2864740 May 29 '20 at 00:28
  • https://stackoverflow.com/q/34361379/2864740 , https://levelup.gitconnected.com/arrow-function-vs-regular-function-in-javascript-b6337fb87032 , https://dmitripavlutin.com/differences-between-arrow-and-regular-functions/ , https://tech4grasp.com/difference-between-regular-function-and-arrow-function/ – user2864740 May 29 '20 at 00:33
  • arrow functions don't rebind `this`. methods and object literal functions rebind `this`. console.log(this) and you'll see it's bound to the wrong object. if you really want that syntax, save `let _this = this` in the outside scope and then use it within the method – user120242 May 29 '20 at 00:49

1 Answers1

0

this.myFunction = this.myFunction.bind(this);

add this code in the constructor.