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.
If you land here in 2015: The real answer here is to use none of these hacks (I dare name them so, because by using any of the methods listed above you will lose the possibility to use resolve and the likes) but to switch to ui-router.
Here's a handy presentation on the differences. Implementation should be as simple as swapping $route for $state and converting the states to names.
I'm currently switching over to a method where i will refer with an a href to a route, with an optional get parameter that changes the state without reloading it. For more on this, look at the 'params' section here
define a factory to handle HTML5's window.history like so (note I keep an own Stack to make it work on android as well since it has a few issues there):
add a few listeners on your dependent scopes and you will be fine like so:
thats how I do it. In my point of view the URL should only change when a new view is loaded. I think thats how the Angular Team intended it anyways. If you want to map forward/back button on your model try to do it with HTML5's window.history
Hope I could help. Cheers, Heinrich
Edit
Better approach when using ngRoute:
How to use:
Old answer
I have written a reusable factory for that based on Jens X Augustsson answer:
Works with AngularJS 1.0.6
How to use:
AngularJS issue here: https://github.com/angular/angular.js/issues/1699
If you set reloadOnSearch to false, you can set the
?a=b&c=d
portion of the url without reload. You can't change the actual location prettily, though, without a reload.Use:
This will prevent reloading the controller on query parameter change, but also on hash change.
Here is plugin: https://github.com/anglibs/angular-location-update
Usage: