Answer the question
In order to leave comments, you need to log in
What is the best way to store data received via Ajax in Angular?
Good day.
I am developing a web application in Angular 4, after authorization, the user receives an array of data from the API via Ajax, for example, a list of his articles. At the moment, with each routing, a service is being processed that re-requests data via api with the same articles, and in my opinion this is somehow not correct, since the chance that they have already changed so quickly in the database is minimal.
In this case, what is the best way to organize data storage and its periodic synchronization with the database? As an option, I am considering LocalStorage, but I would like to know exactly how this is done correctly, and not be based on guesswork.
Thank you.
Answer the question
In order to leave comments, you need to log in
In general, such problems are solved by caching. But as you know, there are two problems in programming - cache invalidation and variable naming. To work correctly with the cache from the frontend, the backend must support things like HTTP Cache (see https://developer.mozilla.org/en-US/docs/Web/HTTP/... there is a translation into Russian, for example) . I'm sure there is native support for Angular 4 (see https://angular.io/guide/http#caching ) or with third party libraries.
I'll add to Ivan Minakov
Service - singleton, can store but throughout the session. Local storage or cache is needed for storage between sessions.
In my application, it was decided this way - if the data is in CouchDb, we issue it, if not, the application climbs onto the server. this solution is relevant for a relatively large amount of data, in addition, you can use localStorage, sessionStorage. As mentioned above, you can of course store data in a service, but storing data in application memory is a bad idea.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question