-2

In Angular 2 after deleting of an item in *ngFor (The item is deleted in the back-End) the item is still visible in the view... DOM is not updating... After the page refresh, the item is not visible in the view...

Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42

2 Answers2

0

After deleting the item from the backend, either you need to do any of the below two. 1. Call the API to fetch the items from the backend. 2. If you really don't want the backend call to fetch the new list after deletion, atleast splice / slice the item from the list, so that *ngFor will not have the item on the list.

Otherwise you will have the item in the list till the list is refreshed.

0

Try something like this:

this._service.delete(item)            
    .subscribe(
        (result) => { this.something = result },
        error => { this.errorMessage = <any>error; },
        () => {
            this.refreshView();         
});

   refreshView() {
    this._service.getItems()            
        .subscribe(
            (result) => { this.myItems = result },
            error => { this.errorMessage = <any>error; },
            () => {});
}
Joe Belladonna
  • 1,349
  • 14
  • 20
  • Hii Joe, Thanks It is Working for me Clearly...And I had a doubt that every time i delete an item i have to make two service calls ...Is it necessary?.. Is there any way to reduce making HTTP calls... – Priyatham krishna May 15 '18 at 06:28
  • @Priyatham krishna I suppose, that you keep your items in some array of items. If you don't want to call server twice for every deletion, you can delete item on server and in the same call, you can splice that item from that array. That way both server and client will have same data. Me personaly, would call server, becuse it is possible that some othet user has also deleted something, and you want to have the fresh data. – Joe Belladonna May 15 '18 at 06:40
  • @Priyatham krishna If this has helped you will you consider accepting my answer and upwote it? – Joe Belladonna May 15 '18 at 07:17
  • Hii Joe, I had planned to delete the items in your way...I'm trying to upvote your answer but it's not considering my vote... – Priyatham krishna May 19 '18 at 04:36