![](/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.