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.
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);
}
}
Sometimes you won't be able to use an arrow function, fe when it's an inbuilt or library function. The solution you can use in this case is bind the this
variable to a variable called self
or whatever you want.
funcA(){
// bind the this variable to 'self' outside of the function scope
var self = this;
var options = {
onSubmit: function (text) {
// and use self instead of this
self.funcB(text)
},
this.nativeKeyboard.showMessenger(options)
}
funcB(text){
alert (text);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.