Volodya-NRG2017-04-20 23:42:28
Volodya-NRG, 2017-04-20 23:42:28

Angular2, how to save service variables when changing route?

Hello. I'm trying to set up an admin panel on Angular2.
1. Route: comments . Here, a list is loaded from the server of all comments and stored in the service property (httpService). I decided to use this class as a container where the data coming from the server will be stored. I then pass this service to the necessary components.
2. Route: comments/:id/edit . This is the page for editing this or that comment.
At the beginning, in the /comments route, I load the list, when switching to comments/:id/edit I want to take the necessary object from the previously created list in order to be able to edit in the form. But when you switch to editing the list is no longer there, i.e. the service did not save or reset the property.
What do you suggest?
(after a couple of days)
this is how it seems to be solved

Answer the question

In order to leave comments, you need to log in

3 answer(s)
sutkin main, 2017-04-21

you can use localStorage.setItem('list', 'listings')
when changing routes just localStorage.getItem('list');
only when you get back to the list router don't forget to refresh localStorage.removeItem('list')
Otherwise you can use service as a global object Service(List) List.listings = 'Listings'
I hope I could help you

coderlex, 2017-04-21

And your service was definitely attached to the application, and not to some separate component like a route controller? Look, maybe it is registered in providers for some component.

Volodya-NRG, 2017-04-21

Or maybe it would still be logical to store data in a global regular variable?
Can't a service in its form be passed from component to component in its new version...
This problem does not even arise in such frameworks as Backbone, Marionette, and here (Angular2) like snow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question