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.
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:
..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:
Getting ugly...
Brief Answer:
You can use the
$location.search()
method as you mentioned. You should listen to the"$routeUpdate"
event on scope instead of other route events. $route API.Explanation:
First of all (you already know), add
reloadOnSearch: false
to your$routeProvider
:Change your anchor tag
href
orng-href
tohref="#/somewhere?param=value"
this will trigger$routeChangeSuccess
event if the path part (/somewhere
) is not the same as current location. Otherwise it will trigger$routeUpdate
event.Listen event on scope:
If you want to change search params in code, you can use
$location.search()
method. $location.search API.This simple solution (surprisingly) works for me:
It doesn't prevent the state reloading on Back and forward button though. Note: I'm using angularjs 1.2.7 and ui-router 0.0.1 (I know it's old).
You can do this without the
$locationChange~
andHistoryState
hacks usingroute
sresolve
promise option.Assuming you had an
article
route where that number is what changed you could do this;