I'm trying something very simple with Angular and TS for an Ionic practice. I have a simple button that when clicked changes a text, and then after a few seconds I want that text changed again.
I'm confuse as to why "this.text" wont work depending on how the timeout function is used.
This doesn't work. (this.text is undefined)
export class HomePage {
constructor() { }
text = "Default";
onChangeText() {
this.text = "Changed!";
setTimeout(
this.onBackToDefault
, 2000);
}
onBackToDefault() {
this.text = "Default";
}
}
While this works
export class HomePage {
constructor() { }
text = "Default";
onChangeText() {
this.text = "Changed!";
setTimeout(() => {
this.onBackToDefault();
}
, 2000);
}
onBackToDefault() {
this.text = "Default";
}
}