0

We want to implement pagination (or filtering) using ui-router. We don't want to reload controller each time next/prev parameters changed. We want to listen for an event and load data from API with new state parameters.

Code from our routes.js

  $stateProvider.state('app.company.account.charges.index', {
    url: '?before&after',
    controller: 'ChargesCtrl',
    templateUrl: 'app/charges.html',
    reloadOnSearch: false,
    params: {
      before: { value: null, squash: true },
      after:  { value: null, squash: true },
    }
  });

our controller:

function ChargesCtrl() {
  var loadCharges = function() {
    Charge.query({before: $state.params.before, after: $state.params.after}).then(function(charges) {
        $scope.charges = charges;
    });
  }

  $scope.goNext = function() {
    $state.go('app.company.account.charges.index', {before: $scope.pagination.before, after: null});
  };

  $scope.goPrevious = function() {
    $state.go('app.company.account.charges.index', {after: $scope.pagination.after, before: null});
  };

  $scope.$on('state params were changed', function() {
    loadCharges();
  });
}

how can we track state params changes if $stateChangeSuccess is not raised because of reloadOnSearch is false.

P.S. now we just broadcast own event to solve this, but I'm looking for 'natural' solution :)

2

2 Answers 2

1

Ui Bootstrap has a really easy way to implement pagination. And it's also easy to integrate with Ui-router! This link will bring you to the pagination help :) have fun coding

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

1 Comment

Tiziano, thanks for your answer. But it's not directly about pagination. As I said we can raise event and do what we need. We don't need to add additional code.
0

Thanks to @krish I have found the answer: Set URL query parameters without state change using Angular ui-router

And it's to use a hack with changing reloadOnSearch option to false before updating location params and then revert it back in $timeout.

However I think that using something like this is more explicit:

$scope.goNext = function() {
    $state.go('app.company.account.charges.index', {before: $scope.pagination.before, after: null});
    $scope.$emit('charges.paginated');

};

$scope.$on('charges.paginated', function(event) {
    event.stopPropagation();

    loadCharges();
});

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.