0

It's possible in angular to save the context of a page ?

I will explain :

I have a page with a component

/cars

This page contains a list of cars got from an $http request to the server

Car 1 (click to see details)

Car 2 (click to see details)

Car 3 (click to see details)

Car 4 (click to see details)

When I click on a car I am redirected to a new page (the detail of selected car)

/details/1 // 1 is the id of a car

And if I go back to the list of cars, the component will send an $http request again to get the list of cars.

My question is, it's possible when I go back, to render the previous context without retrieving data from the server ?

One option is to use services to store data.

There is any better idea ?

(Jira and GitLab are using this UX behavior ...)

Thank you !

2 Answers 2

1

Using a shared service is the generally recommended way to do this.

One option is to cache it in the service that makes the HTTP request, so that the structure of your app remains the same, and only the service changes (make the request once, cache the results, and after that, always return the cached values).

If this data can change while the user is interacting with the app, then you will need to figure out when to invalidate the cache.

Sign up to request clarification or add additional context in comments.

Comments

0

You can save the client data on the localstorage.

For example

localStorage.setItem('car', 'something');

It's pretty widely supported.

Make sure to stringify value:

localStorage.setItem(itemName, JSON.stringify(itemData));

You can use Angular2 @LocalStorage. It's a little Angular2/typescript decorator that makes it super easy to save and restore automatically a variable using HTML5' LocalStorage.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.