AngularJS Paging With $location.path But No NgView Reload
Answer :
Instead of updating the path, just update query param with a page number.
set your route to ignore query param changes:
.... $routeProvider.when('/foo', {..., reloadOnSearch: false}) ....
and in your app update $location
with:
... $location.search('page', pageNumber); ...
From this blog post:
by default all location changes go through the routing process, which updates the angular view.
There’s a simple way to short-circuit this, however. Angular watches for a location change (whether it’s accomplished through typing in the location bar, clicking a link or setting the location through
$location.path()
). When it senses this change, it broadcasts an event,$locationChangeSuccess
, and begins the routing process. What we do is capture the event and reset the route to what it was previously.
function MyCtrl($route, $scope) { var lastRoute = $route.current; $scope.$on('$locationChangeSuccess', function(event) { $route.current = lastRoute; }); }
My solution was to use the locationChangeSuccess.
The benefit is being able to access the "params" property on both "next" and "last" routes like next.params.yourproperty
when you are using the "/property/value" URL style and of course use $location.url
or $location.path
to change the URL instead of $location.search()
that depends on "?property=value" URL style.
In my case I used it not only for that but also to prevent the route to change is the controller did not change:
$scope.$on('$routeChangeStart',function(e,next,last){ if(next.$$route.controller === last.$$route.controller){ e.preventDefault(); $route.current = last.$$route; //do whatever you want in here! } });
Personally I feel like AngularJS should provide a way to control it, right now they assume that whenever you change the browser's location you want to change the route.
Comments
Post a Comment