Reputation: 433
I am beginner to angular so I will need a little help with this: I have got PageA and PageB. PageA has variables like a:number, b:string, c:OwnType... The user changes some values of theese vars. Then he gets redirected by a link to PageB. He realises that he missed something on PageA so he will press the back state button of the browser. Unfortunatelly his changes on the attributes are lost so he needs to start work again on PageA from the begining.
How can I get PageA to save the modified values on destroy and load them on init from somewhere if they were set once?
I can use Local/Session storage or a service for this. On the other hand I want to know if there is something better solution then these. Thanks your time and answers :)
(I dont want to store something like states on the server)
Upvotes: 0
Views: 89
Reputation: 3192
You can use services to store the values and retrive it when needed. As you might be knowing already, services are singleton in nature. So you can use them as a cache and use them effectively when needed. An example is shown below (Not a tested code)
// Component
ngOnInit() {
let values = this.someService.getValues();
if(values) {
this.classPropertyA = values.a;
}
...
}
ngOnDestroy() {
this.someService.setValues(value1,value2); //Whatever values you want to store
}
This might not handle reload conditions though. For that you might have to use local storage or some other storage methods. Hope this helps.
Upvotes: 1