0

I have created a factory as a service to get some data from server using Web API call, but if i need to get same data again, I need to hit API again, I want to use same data in further places and don't want to make Web API call again.

Is there any way to persist data in angular js?

1
  • You can go for sessionStorage to store the result from Api and while you need the same data again then, check the sessionStorage has the data and use if not then make a call again and store it to the same sessionStorage in controller level. It may save your Unnecessary Api Call when you have with your local. Commented Apr 21, 2017 at 9:11

3 Answers 3

1

Ways to sharing the data across the application :

1. Using service

We can create a service to set and get the data between the controllers and then inject that service in the controller function where we want to use it.

Service :

app.service('setGetData', function() {
  var data = '';
    getData: function() { return data; },
    setData: function(requestData) { data = requestData; }
});

Controllers :

app.controller('myCtrl1', ['setGetData',function(setGetData) {

  // To set the data from the one controller
  var data = 'Hello World !!';  
  setGetData.setData(data);

}]);

app.controller('myCtrl2', ['setGetData',function(setGetData) {

  // To get the data from the another controller  
  var res = setGetData.getData();
  console.log(res); // Hello World !!

}]);

Here, we can see that myCtrl1 is used for setting the data and myCtrl2 is used for getting the data. So, we can share the data from one controller to another controller like this without making any further API call.

2. Using Session storage

Create factory service that will save and return the saved session data based on the key.

app.factory('storageService', ['$rootScope', function($rootScope) {

    return {
        get: function(key) {
            return sessionStorage.getItem(key);
        },
        save: function(key, data) {
            sessionStorage.setItem(key, data);
        }
    };
}]);

controller :

Inject the storageService dependency in the controller to set and get the data from the session storage.

app.controller('myCtrl',['storageService',function(storageService) {

  // Save session data to storageService
  storageService.save('key', 'value');

  // Get saved session data from storageService
  var sessionData = storageService.get('key');

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

Comments

1

if you're making an async call inside your service and the data you're fetching are "static" it makes sense to cache them to avoid a new overhead each time you need them. Cache is disabled by default.

$http({
 method: 'GET',
 cache: true
}

2 Comments

if we have thousands of record as JSON data, will It good to cache it?
it's a mechanism inside the $http service that store a variable with the result and avoid making a new call each time you hit that service again. it's perfectly legit.
0

You can use HTML5 Web Storage for that.

localStorage.setItem("lastname", "Smith");
console.log(localStorage.getItem("lastname"));

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.