J
J
Jekins D&D2017-08-16 23:27:34
JavaScript
Jekins D&D, 2017-08-16 23:27:34

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

5 answer(s)
I
Ivan Minakov, 2017-08-17
@Hardwit

Use your service to store data, don't ask again.

X
xolvo, 2017-08-17
@xolvo

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.

V
Vladimir, 2017-08-17
@Casufi

I'll add to Ivan Minakov
Service - singleton, can store but throughout the session. Local storage or cache is needed for storage between sessions.

A
Andrey Shedko, 2017-08-24
@Mamboking

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.

R
Roman, 2017-08-29
@uaKorona

If you have a root route, you can resolve data (a list of articles) at its level - and they will be available on all nested routes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question