Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Change hash and route without completely reloading controller

I have a controller, with a route like this:

#/articles/1234

I want to change the route without completely reloading the controller, so I can keep the position of other stuff in the controller constant (lists that scroll)

I can think of a few ways to do this, but they're all pretty ugly. Is there a best practice for doing something like this? I tried experimenting with reloadOnSearch: false, but it doesn't seem to change anything.

like image 546
doubledriscoll Avatar asked Aug 24 '12 19:08

doubledriscoll


1 Answers

Had the very same challange,

Found a hack in another StackOverflow response that did the trick

Fairly clean solution - all I did was to add these lines to the controller that sets $location.path:

var lastRoute = $route.current; $scope.$on('$locationChangeSuccess', function(event) {     $route.current = lastRoute; }); 

..and made sure $route in injected into the controller of course.

But still, feels like "DoNotFollowRoutesOnPathChange" is a missing feature in AngularJS.

/Jens

Update: Since listening to this event effectively kills further usage of $routeProvider configs, I had to limit this catch to current path only:

    var lastRoute = $route.current;     if ($route.current.$route.templateUrl.indexOf('mycurrentpath') > 0) {         $route.current = lastRoute;              } 

Getting ugly...

like image 100
Jens X Augustsson Avatar answered Sep 20 '22 23:09

Jens X Augustsson