3

I'm trying to figure out the best way to implement the following scenario using AngularJs routing:

  1. Users have a list of items with search capability (e.g. movies).

  2. Users can search and select item, click on it and view details in a separate view.

  3. Once they finish viewing the item they can click browser back button and return to previous list with restored search options to continue viewing the list.

Default ngRoute doesn't provide any state concept and this has to be implemented separately (I wonder what is the best solution here)

Use ui-router. I've never worked with it and I wonder if it provides such functionality out-of-the-box

1
  • You can clear cache for that route template on back button as ionic does by deleting history. Commented May 29, 2015 at 14:53

2 Answers 2

1

For the back button to go back to the same state where the page was in your search screen save the state of search parameters, pagination etc in the URL:

 $location.search({"searchTerm": "jaws", "genre": "fishy", page: 3});

This will add ?searchTerm=jaws&genre=fishy&page=3 to you URL and reload controller. At the start of controller set variables from 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 they click off to the result the back button will work as normal and reload the controller and with the parameters.

If you don't want the browser history to have every action taken in the search screen and only want the last state recorded in the history use replace:

 $location.search({"searchTerm": "jaws", "genre": "fishy", page: 3}).replace();

Now the history will have just a single entry for the search screen regardless of the history of options chosen.

Sign up to request clarification or add additional context in comments.

Comments

0

ui-router provides functionality to implement this easily. Here is complete post about this:

http://www.codelord.net/2015/06/20/simple-pagination-and-url-params-with-ui-router/

So, the important things are:

  • Configure the state in your router to receive the page parameter
$stateProvider.state('list', {
  url: '/list?page',
  controller: 'ListCtrl',
  controllerAs: 'list',
  templateUrl: 'list.html',
  params: {
    page: {
      value: '1',
      squash: true
    }
  }
});
  • Handle the page parameter when the controller is initializing and update the state on every change of page:
angular
  .module('your.module')
  .controller('ListCtrl', function ($state, $stateParams) {
    var vm = this;
    vm.page = parseInt($stateParams.page || '1'); // page param is a String
    loadStuffForList(); //Load elements for list

    vm.onPageChange = function () {
      $state.go('.', {'page': '' + vm.page});
      // or
      // $state.go('.', {'page': '' + vm.page}, {'notify': false}); // notify: false -> in case you need it, it will prevent the controller from reloading
      // loadStuffForList();
    };
    function loadStuffForList () {...};
  });

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.