I'm working on a currency converter, and I have url like #/currency/50/USD/to/EUR, where 50, USD and EUR are parameters. Now I have a switch function which swaps the currencies and keep the value to convert, I'd like to change the URL as well to something like #/currency/50/EUR/to/USD without reloading the controller, just change the hash. I have an idea on how to do with pure js, but is there any solution in angular way?
2 Answers
Check this answer out. It sounds just like what you need.
Basically, you seem to have two choices:
- Listen for
$locationChangeSuccessevent. - In your
$routeProviderdefinition, while defining the routes and the corresponding templated to load, specify the optionreloadOnSearch = false.
1 Comment
Jonathan de M.
Solution one works, but I had to do some tweaks to load other controller when needed.
Ok following callmekatootie solution here's my result, I had to add a condition to be able to go out of my controller when required
controller('CurrencyConvertCtrl', function ($scope, $route){
var lastRoute = $route.current;
$scope.$on('$locationChangeSuccess', function(event) {
if($route.current.$$route.controller === 'CurrencyConvertCtrl'){
// Will not load only if my view use the same controller
$route.current = lastRoute;
}
});
}
2 Comments
parliament
breaks the back button
doublesharp
I was using something similar which was not changing the route but was reloading the controller. Adding
event.preventDefault() fixed this for me.
ui-router!