I am writing an app with Ionic 2 and am making good progress, but I just got hung up on something that I think is probably a simple fix.
I have text on the screen, after 5 seconds I want to update the text on the screen. I seem to lose access to the variable within the setInterval function.
constructor(public nav: NavController, private navParams: NavParams, private getserver: GetServerService, storage: Storage) {
this.storage = storage;
this.storage.get('userAuth').then((name) => {
if(name.id === undefined) {
this.nav.setRoot(LoginPage); //kick back to login if no userdata
}else{
let options = {timeout: 120000, enableHighAccuracy: true};
navigator.geolocation.getCurrentPosition((position) => {
this.driverStatus = 'hi';
setInterval(()=> {
this.driverStatus = 'Updated Text';
}, 2000);
},
(error) => {
console.log(error);
},
options
);
}
});
}
}
When I run the app, it shows the text "Hello" but never updates the text to "Updated Text". The console shows the output properly.
I have tried suggestions from Ionic2 function setInterval can't get a parameter but those suggestions did not work either. My other failed attempts are below.
var that = this;
setInterval(function() {
that.userText = 'Updated Text';
}, 2000);
and
setInterval(()=> {
this.userText = 'Updated Text';
}, 2000);