How to update local data using $ resource service in AngularJS

I have an AngularJS application that uses $ resource service to retrieve data using the query () method and create new data using the model. $ Save () method. This works great, as the docs say.

My question is how to update my local data received with MyService.query (), first of all, after I changed it?

Now I have used the simplest approach, and I call the query () method again. I know that this is the worst way in terms of efficiency, but it is the easiest.

In my server side, I am returning all state representation of the new model. How to add a new model to an array of local data?

UPDATE
I end up just pushing the model back from the server, but I will still be happy to know if this is the way to go. From what I can understand from the source code, the returned array is a plan-old-javascript array that I can manipulate myself.

This is the code I used

$scope.save = function () { var newComment = new CommentsDataSource(); newComment.Content = $scope.todoText; newComment.$save({ id: "1" }, function (savedComment) { $scope.comments.push(savedComment); }); } 
+4
source share
1 answer

I would just get the whole list again to see the changes made to the list by other users.

But if the solution you use is right for you, then use it. This is fixed. Angular uses bare-bones JavaScript objects. Adding a new instance to the list in the area will update the list displayed on the page.

+3
source

Source: https://habr.com/ru/post/1492465/


All Articles