javascript - AngularJS routing. Save and restore state on back button -
i'm trying figure out best way implement following scenario using angularjs routing:
users have list of items search capability (e.g. movies).
users can search , select item, click on , view details in separate view.
once finish viewing item can click browser button , return previous list restored search options continue viewing list.
default ngroute doesn't provide state concept , has implemented separately (i wonder best solution here)
use ui-router. i've never worked , wonder if provides such functionality out-of-the-box
for button go same state page in search screen save state of search parameters, pagination etc in url:
$location.search({"searchterm": "jaws", "genre": "fishy", page: 3});
this add ?searchterm=jaws&genre=fishy&page=3
url , reload controller. @ start of controller set variables url thus:
var searchterm = $location.search().searchterm; var genre = $location.search().genre; var page = $location.search().page; searchservice.search(searchterm, genre, page, function(data){ $scope.results = data; });
now when click off result button work normal , reload controller , parameters.
if don't want browser history have every action taken in search screen , want last state recorded in history use replace
:
$location.search({"searchterm": "jaws", "genre": "fishy", page: 3}).replace();
now history have single entry search screen regardless of history of options chosen.
Comments
Post a Comment