Reputation: 11198
I have an app and I am utilizing routes to load two different views.
The first route #/
is the main list view. That works every time.
My second route is #/view/:id
and loads my second view when a list item is clicked.
The view is data driven based off the id, all of which need to make $http
requests through a service
I created.
Is it possible that when I leave the #/view/:id
route, it unloads that view completely and reloads it when I come back?
OR
How do I go about resetting promises inside a service so that the next time the view is loaded, it will request the new data?
Upvotes: 1
Views: 361
Reputation: 120513
These are not mutually exclusive questions. Routing with ngView
destroys the view, scope, and controller on every load, so controller and scope history are not preserved among routes. Otherwise, memory load in the browser would be high; instead, we can use services to preserve the state we need to across all routes.
$http
requests are made fresh every time they are called, unless the cache
option is enabled. There shouldn't be any work required for this to work as designed:
.factory( 'dataService', function( $http ) {
return function dataServiceFn ( id ) {
return $http.get( '/myapi', { params: { id: id } } );
};
});
When your data service is called (e.g. dataService( $routeParams.id );
), a new promise will be returned.
This is just a sample service structure for an incredibly simple case, but so long as the $http
call is made fresh on each service call, you will get a new promise guaranteeing fresh data every time.
Upvotes: 2
Reputation: 404
Angular will unload the controller when you leave to another controller. If you need to store the data between different view, you should store it inside the service (which is not unloaded when the controller changes) or be prepared to make an $http request every time you load the view.
Upvotes: 1