There are several methods and two seemed to be quite reliable. I ended up choosing the first approach for my app since my search parameters needed to propagate to other controllers.
Storing them in cookies
Angular.js offers $cookies that allows to get/set parameters on the browser. I use this as true source of search params.
For example:
search.service.js
angular
.module('app')
.service('SearchService', SearchService);
SearchService.$inject = [
'$cookies'
];
function SearchService(
$cookies
) {
var searchCookieKey = 'searchHistoryCookieKey';
var searchCookieMaxSize = 10;
return {
search: search,
getSearchHistory: getSearchHistory
};
function search(arg1, arg2) {
storeSearchHistory({arg1: arg1, arg2: arg2});
// do your search here
// also, you should cache your search so
// when you use the 'most recent' params
// then it won't create another network request
}
// Store search params in cookies
function storeSearchHistory(params) {
var history = getSearchHistory();
history.unshift(params); // first one is most recent
if(history.length > searchCookieMaxSize) {
history.pop();
}
$cookies.putObject(searchCookieKey, history);
}
// Get recent history from cookies
function getSearchHistory() {
return $cookies.getObject(searchCookieKey) || [];
}
}
app.states.js
.state('search', {
url: "/search",
templateUrl: "/dashboard/search/templates/index.html",
controller: 'SearchController',
resolve: {
searchResults: ['SearchService', '$stateParams', function(SearchService, $stateParams) {
if(!$stateParams.arg1 || !$stateParams.arg2) {
var history = SearchService.getSearchHistory();
var mostRecent = history.length ? history[0] : null;
if(mostRecent) {
return SearchService.search(mostRecent.arg1, mostRecent.arg2);
}
}
return SearchService.search($stateParams.arg1, $stateParams.arg2);
}]
}
})
If you are not caching these search network calls, then your app will need to wait for the request to return, thus slowing down your app.
Passing them around in states
You can create a parent controller which holds your $stateParams
and your child controllers will inherit the parameters. The parameters are not overwritten when going back/forward or accessing between child states. However, they are overwritten when you move states with specific params. So, you will just need to explicity specify those parent's $stateParams
when moving between states.