4

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
  • 1
    You can also look at ui-router module which can replace the standard routing with state base routing and can support your scenario.angular-ui.github.io/ui-router/sample/# Commented Aug 29, 2013 at 9:08
  • @Chandermani thanks for pointing me to ui-router! Commented Sep 4, 2014 at 8:22

2 Answers 2

13

Check this answer out. It sounds just like what you need.

Basically, you seem to have two choices:

  1. Listen for $locationChangeSuccess event.
  2. In your $routeProvider definition, while defining the routes and the corresponding templated to load, specify the option reloadOnSearch = false.
Sign up to request clarification or add additional context in comments.

1 Comment

Solution one works, but I had to do some tweaks to load other controller when needed.
7

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

breaks the back button
I was using something similar which was not changing the route but was reloading the controller. Adding event.preventDefault() fixed this for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.