0

Am tried to save my http response in to one global variable and if I go to next page and come back again the same page the stored value was got null/Undefined

Now how to save my response array in global and access it back again with out loss in Angular 4.

Thanks.

2
  • Are you getting response from some api? Commented Jul 8, 2020 at 13:05
  • You can use an angular service and save the response in a local variable, then in the other calls you can check if the variable is valorized Commented Jul 8, 2020 at 13:08

5 Answers 5

1

There are many ways to store/save your "response array".

  • if your site is not SPA and you are jumping from one page to another, you can store it in cookies, localStorage or sessionStorage.
  • if you site is SPA you can create a service that will serve that array to any component or further more you can use Redux for Angular.
Sign up to request clarification or add additional context in comments.

Comments

1

create a service in your app and use the below code create a setter and getter method


constructor(private val: any) {}

set val(val){
  this.val = val
}
get val(){
 return val
}
call setter method and update the value whenever you get new data from api



call getter method after  creating an instance of this service in ahy of you component in same module

Comments

0

You create service in angular and use it. Service will hold data globally and updated. Basically you would declare variable in service and access in component and update there. Use where you want to use

Comments

0

You can do it either with service or model.

1)In model,declare a variable of array type and assign the http response to that model variable and import model in your component .

2)In service, create get and set functions and call set function with http response and call get in your desired component file

Either ways you can access data across all modules and components.

Comments

0

I created one service file with set and get functions. At first time it saved and retrieved correctly.

But if I go to next page and come back again the values are reseted and value got empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.