![](/img/trans.png)
[英]ngFor crashes when adding new items to collection after animation in Nativescript Angular
[英]angular foreach problems when adding new items
我有Firebase的角度應用程序。 我想更改firebase項的值。 每當KM總變化時,需要將其設置為正確的值。 所以這是我的服務:
countKm(){
this.totalKmRef = this.db.list(this.uid + '/rides');
this.totalKm$ = this.totalKmRef.valueChanges();
this.totalKm$.subscribe(res=> this.getAllRides(res));
}
getAllRides(res){
this.rides = res;
this.rides.forEach(ride =>{
this.uniqueRides.push(ride.km);
})
this.totalKm = this.uniqueRides.reduce(this.sum);
localStorage.setItem("totalKm", this.totalKm);
}
sum(a, b){
return a += b;
}
現在出於某種原因,當我添加一個騎行一切都很好,但第二次一切都出錯了。
例如我第一次運行foreach(在firebase已經有5個項目之后):
0{
0: 5,
1: 5,
2: 5,
3: 5,
4: 5,
}
在我運行foreach 1次后,我的對象列表如下:0 {0:5,1:5,2:5,3:5,4:5,5:5,}
第二次添加新值后,foreach執行此操作:
0{
0: 5,
1: 5,
2: 5,
3: 5,
4: 5,
5: 5,
6: 5,
7: 5,
8: 5,
9: 5,
10: 5,
11: 5,
12: 5,
}
有人可以幫助我嗎? 我認為這是一個錯誤的配置在foreach但無法找到在哪里。
謝謝你的幫助!
this.uniqueRides.push(ride.km);
只是將其他項目推入陣列。
無論是檢查是否乘坐包括.includes
this.rides.forEach(ride => {
if(!this.uniqueRides.includes(ride)) {
this.uniqueRides.push(ride.km);
}
})
或每次清除陣列。 this.uniqueRides = []
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.