0

This may be a very basic question but I hope the gurus here at Stackoverflow will be able to provide a comprehensive and educating answer.

When I press the back button in my angular app, are the controllers fetch data from the backend again? And is that possible to avoid that, and just load what was in the page previously, including various states it had such as ordering of rows in a table?

Thanks

1 Answer 1

1

When you change routes, your attached controller functions will rerun. Inside your controllers, or services, whatever is fetching the data, you can save contents to a parent scope, such as $rootScope or you can save to the browser session storage, and check to see if either of those things have been populated before fetching data.

function controller ($scope, $rootScope, $http) {
  if (! $rootScope.savedData) {
    $http.get('data').success(function (data) {
      $rootScope.savedData = data;
      $scope.data = data;
    }); 
  }
  else $scope.data = $rootScope.savedData;
}
Sign up to request clarification or add additional context in comments.

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.