0
I’m new to Angular.
One of the main goals of async pipe is to automatically take care of signing up (subscribe) and unsubscribe (unsubscribe).
I have two doubts:
First Doubt
When dealing with delete or update (both are API services), 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();
}
});
}
}
Second Doubt
There is another way to delete a specific element of a Observable
without calling the API that loads the projects$
? Something below, different with the top code:
//Neste caso this.projectsects type não é um 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();
}
});
}
}