angular - Array not getting spliced in DOM but in console it is - Ionic 2+/Angular2+ -
i have dynamic array full of items , values. when user clicks button on item, should remove item view list. i'm out of guesses why is. how data structured? wouldn't think because shows it's being removed in console. anyways thanks!
ts:
export class page{ items: item[]; constructor(public alertctrl: alertcontroller){} removeitem(i) { let confirm = this.alertctrl.create({ title: 'confirm', message: "text.", buttons: [ { text: 'cancel', handler: () => { console.log('disagree clicked'); } }, { text: 'save', handler: () => { this.presenttoast() this.items.splice(i, 1); } } ] }); confirm.present(); } getitems(){ this.stopservice.getitems().subscribe(item => { this.items = item }) } } html:
<div *ngfor="let item of items; index "> <h3>{{item.description}}</h3> <button item-end ion-button full (click)="removeitem(i)">remove item</button> </div> edit
adding service how items --
getitems(): observable<any> { return this.http.get(someendpoint) .map(res => res.json()); }
try following:
removeitem(i) { let confirm = this.alertctrl.create({ title: 'confirm', message: "text.", buttons: [ { text: 'cancel', handler: () => { console.log('disagree clicked'); } }, { text: 'save', handler: () => { this.presenttoast() this.items = [...this.items.filter((item, index) => index !== i]; } } ] }); confirm.present(); } this changes object reference , should trigger dom update.
if not work, try wrap splice in settimeout:
settimeout(() => { this.items.splice(i, 1); }, 0); you can try inject public zone: ngzone in constructor , run splice in this.zone.run(() => { this.items.splice(i, 1); });. way of forcing change detection.
edit:
in getitems() method, try this:
getitems() { this.stopservice.getitems().subscribe(item => { this.items = [...item]; }); } plunker reference:
Comments
Post a Comment