I'm currently a newbie with Angular.
One of the main goal of async pipe is automatically take care of subscribing and unsubscribing for you.
1. When dealing with delete or update (both are API services), Should I need to unsubscribe via ngOnDestroy?
projects$: Observable<Project[]>;
constructor(
private projectService: ProjectService,
) { }
ngOnInit() {
this.loadProject();
}
loadProject(){
this.projects$ = this.projectService.getProjects()
.pipe(
map(result => {
if(result.success){
return result.data;
}
})
);
}
deleteProject(){
if(this.projectToDelete){
const id = this.projectToDelete.id;
this.projectService.deleteProject(id).subscribe(result => {
if(result.success){
this.loadProject();
}
});
}
}
Is there another way to delete a specific element from Observable without calling again the api that refreshs the
projects$
? Something like this, instead with the one above://In this case this.projects type is not an Observable. deleteProject(){ if(this.projectToDelete){ const id = this.projectToDelete.id; this.projectService.deleteProject(id) .subscribe(result => { if(result.success){ this.projects = this.projects.filter(item => { return item.id != id; }); this.projectToDelete = null; this.closeModal(); } }); } }