Search code examples
angulartypescriptionic2ionic3angular2-components

How to call parent function from callback function in angular 2?


Here is a class

export class ChatDetailPage {

constructor(){

}

funcA(){
     var options = {
        onSubmit: function (text) {
        //i want to be able to access funcB from here 
        this.funcB(text)
          },
this.nativeKeyboard.showMessenger(options)
}

funcB(text){
  alert (text);
}

}

Here in this case how can i call funcB from onsubmit callback function in Anular 2 or Ionic 3.

Thanks in advance.


Solution

  • Use an arrow function, which captures the value of this:

    export class ChatDetailPage {
    
        constructor(){
    
        }
    
        funcA(){
           var options = {
               onSubmit: text => {
                  //i want to be able to access funcB from here 
                  this.funcB(text)
               },
           };
           this.nativeKeyboard.showMessenger(options)
       }
    
       funcB(text){
          alert (text);
       }
    }