我有這樣的for循環中檢索,我檢索用戶的進度For循環只顯示從火力
打字稿最後一個數據:
his.userProgress = af.object('/UserProgress/' + this.currentUser + '/', { preserveSnapshot: true });
this.userProgress.subscribe(snapshots => {
snapshots.forEach(snapshot => {
this.userScores.push(snapshot.val());
});
console.log(this.userScores);
//set this one to i<=8 because total no. of quizzes are 9. which means this.userScores.length -1
for(var i=0; i <= 8; i++){
if (this.userScores.length == i) {
this.scores = [{
None: "Nothing Recorded"
}];
console.log("Nothing");
}
else if (this.userScores.length >= i) {
this.scores = [{
Chapter:this.userScores[i].Chapter_Quiz,
QuizNo:this.userScores[i].Quiz,
Score:this.userScores[i].Score,
}];
console.log("With Scores");
}
}
});
首先,它會檢查是否存在userScores []長度不0或大於或等於0.如果該測驗沒有分數,則將顯示「無記錄」,否則將顯示分數。
HTML:
<ion-card>
<ion-list *ngFor="let score of scores">
<ion-card-header>
{{score.Chapter}}
</ion-card-header>
<button ion-item *ngIf="scores.length < 0">
<ion-icon name="planet" item-start></ion-icon>
{{score.None}}
</button>
<button ion-item *ngIf="scores.length >= 0">
<ion-icon name="planet" item-start></ion-icon>
{{score.Score}}
</button>
</ion-list>
我有問題
在那裏只顯示最後一個記錄。我做錯了什麼?
預期輸出:
If finished with the 1st quiz:
1st: Score
2nd: Nothing Recorded
3rd: Nothing Recorded
....
if no score at all:
1st: Nothing Recorded
2nd: Nothing Recorded
.....
哪一個日誌顯示? – Vega
@Vega它只顯示MitAdapt。我從我的console.log中添加了輸出 – AngularNewbie