-2
Assuming I have a table showing data from an array, when entering a new element in the database via a request POST
, how best to update the data on screen?
I’m always in doubt between making a GET
again, to then include the new records:
this.service.save(element).subscribe(res => {
this.getLista()
});
In this way I believe that it has the advantage of inserting beyond the element that has just been created, the element that other people created between the moment of the save
and the call from get
but it would be an extra request and depending on the amount of data.
or take the return of the api, which is usually the element that was created, and insert into the arrray:
this.service.save(element).subscribe(res => {
this.array.push(res.data)
});
This way adds instantly without depending on the server, but it may be that the list is not fully updated.
Which of the two ways is the most recommended to update data that has just been added/deleted/updated?
Your question face is wide and mainly depends on each case, I always do a get to make sure to pick up all the changes, the second way do not know if you would be with the list 100% updated, only that would do different, I would do the get on ngOnInit() and in the post if all goes well then I give a ngOnInit() to reload and update the component
– LeAndrade