Hash change without reboot controller in AngularJS

I am working on a currency converter and I have a url like #/currency/50/USD/to/EUR , where 50, USD and EUR are parameters. Now I have a switch function that changes the currency and saves the value for conversion. I would also like to change the URL, for example, #/currency/50/EUR/to/USD without rebooting the controller, just changing the hash. I have an idea on how to do with pure js, but are there any solutions in angular?

+4
source share
2 answers

Mark the answer . It sounds the same as what you need.

Basically, you seem to have two options:

  • Listen to the $locationChangeSuccess event.
  • In the definition of $routeProvider , specifying the routes and corresponding templates for loading, specify the option reloadOnSearch = false .
+13
source

Ok after solving callmekatootie here is my result, I had to add a condition so that I could exit 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; } }); } 
+7
source

Source: https://habr.com/ru/post/1499560/


All Articles