Reputation: 3
I just started writing an application with AngularJS and I have a couple questions about data persistence across routes. Here a plunker of a simple list application that demonstrates my question. Right now, each time a controller is loaded an HTTP request is being made. I only want a single HTTP request to be sent when the app is loaded and then be able to use that data in both of my views.
Question 1: When I click on a list item, how can I use the data that was loaded in ListCtrl rather than issuing another request to pull that same data(but for a single item)?
Question 2: If I've click on an item and then click the back button, how can I make it so that ListCtrl doesn't re-issue a request to get all the list items?
Upvotes: 0
Views: 48
Reputation: 1924
You are probably looking for the $cacheFactory. You can use it to construct cache objects to store your data.
Also, this guy has a pretty good post on using it for what you are looking for, so I won't reinvent the wheel here.
http://www.phase2technology.com/blog/caching-json-arrays-using-cachefactory-in-angularjs-1-2-x/
Upvotes: 1
Reputation: 1571
Answer 1 : You can save the data on $rootScope
so the data accessible by all $scope
.
Answer 2 : You can cache
your ajax so AngularJS will only request the data at first service call, and use it for the rest service call.
Upvotes: 0