I've built a CRUD app which communicates with a REST api but haven't found a way to refresh the view after one item is deleted. I've used router.navigate in the past to change views after other methods such as the create method and this works fine.
The problem is that the event which calls the delete method is inside the same listing of the items (every *ngFor item has its own delete event). So if I delete an item and then I use router.navigate to go to the current view it does nothing because you are already there, thus not seeing updated version of the view without the deleted item even if it has worked.
Is there any other way to refresh a view without using route.navigate?
Edit:
I've tried to implement ChangeDetectorRef, still not working though...
Here's the component:
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { ApiNoticiasService } from './api-noticias.service';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
moduleId: module.id,
selector: 'app-noticias',
templateUrl: 'noticias.component.html',
styleUrls: ['noticias.component.css'],
directives: [ROUTER_DIRECTIVES],
providers: [ApiNoticiasService]
})
export class NoticiasComponent implements OnInit {
noticias: any;
constructor(
private cd: ChangeDetectorRef,
private _apiNoticias: ApiNoticiasService) { }
ngOnInit() {
this._apiNoticias.getNoticias()
.then( (noticias) => this.noticias = noticias)
.catch((err) => {
console.log(err);
});
}
deleteNoticia(id){
this._apiNoticias.deleteNoticia(id)
.catch((err) => {
console.log(err);
});
this.cd.markForCheck();
}
}
And this is the method in the service:
deleteNoticia(id) {
return this._http.delete('http://localhost:3000/noticias/' +id)
.map((response: Response) => response.json())
.toPromise()
.catch((err: any) => {
console.log(err);
return Promise.reject(err);
});
}