angular - Array is duplicating when adding a new item -
i have ionic2 app using firebase backend. in ionic app have product page, when add new product in control panel , push firebase, if viewing product page in ionic app, new product added, items in list duplicated also.
if add new item when user viewing product list, see whole list duplicating, if leave screen , visit again, list normal 1 of each item.
this code using in ionic app pull products through firebase.
public menuitems: array<any> = []; public selecteditems: array<any> = []; menuitem: firebaselistobservable<any>; this.id = this.navparams.get('id'); this.menuitem = af.database.list('/menuitems', { query: { orderbychild: 'category', equalto: this.id } }) this.menuitem.subscribe((data) => { this.menuitems = data; (var = 0; <= this.menuitems.length - 1; i++) { if (this.menuitems[i].category == this.id) { this.selecteditems.push(this.menuitems[i]); this.items = this.selecteditems; } } }) an example:
if have list of items so:
- item1
- item2
- item3
when add new item dashboard, , push firebase, ionic app show following:
- item1
- item2
- item3
- item4
- item1
- item2
- item3
- item4
i not sure why doing this, appreciated.
quite simple. have little tweaking.
change this:
(var = 0; <= this.menuitems.length - 1; i++) { if (this.menuitems[i].category == this.id) { this.selecteditems.push(this.menuitems[i]); this.items = this.selecteditems; } } into:
let selecteditems: any[] = []; (var = 0; <= this.menuitems.length - 1; i++) { if (this.menuitems[i].category == this.id) { selecteditems.push(this.menuitems[i]); } } this.items = selecteditems;
Comments
Post a Comment