I have an app for language school. There are several Levels (A1, A2, B1…) and each Level has several Lessons. On the home page I’m displaying all the levels, and when the user clicks one of them, he should be taken to a page with all the lessons for that level. That works the first time, but when I go back and then open another level, I still see the lessons from the first level I’ve chosen. In the logs I see that my REST service was called only the first time. I have to refresh the page (F5), and only then will the REST endpoint be called again for the level that I pick.
Here are the relevant parts of server.js
:
var Level = mongoose.model('Level', {
title: String
});
var Lesson = mongoose.model('Lesson', {
title: String,
body: String,
levelTitle: String
});
app.get('/api/lessons/:levelTitle', function (req, res) {
Lesson.find({ levelTitle: req.params.levelTitle },
function (err, lessons) {
if (err)
res.send(err);
res.json(lessons);
});
});
I call the REST service from LessonProvider
:
getLessonsForLevel(level) {
if (this.data) {
return Promise.resolve(this.data);
}
return new Promise(resolve => {
this.http.get('http://localhost:8080/api/lessons/' + level.title)
.subscribe(data => {
this.data = data;
resolve(this.data);
});
});
}
…and that method is called from level-view.ts
:
constructor(public navCtrl: NavController, public navParams: NavParams, public levelService: LevelProvider, public lessonService: LessonProvider) {
this.level = navParams.get('level');
}
ionViewDidLoad() {
this.lessonService.getLessonsForLevel(this.level).then((data) => {
this.lessons = data;
});
}
I get to the level-view
page via a method in home.ts
:
goToViewLevelPage(level) {
this.navCtrl.push(LevelViewPage, { level: level });
}
…which is called when a user clicks on a button on the home.html
page:
<ion-item *ngFor="let level of levels">
<h1>{{level.title}}</h1>
<button ion-button (click)="goToViewLevelPage(level)">
See lessons for this level
</button>
</ion-item>
Just to be clear, when I navigate to a certain level page, I have the back arrow in the upper-left corner (I think that’s a part of ion-navbar
inside the ion-header
). I use that arrow to go back to the home page and then to pick another level.
Does anyone have a clue what might be wrong?