0

I am implementing authentication checking using $routeChangeStart as explained here and looking for a way to preserve all detail provided in the next object. AngularJS documentation on $route shows that you can set the $route.current and I was hoping that I do something like the following to change the $route instead of using $location:

$route.current = { templateUrl: 'detail.html', controller: 'MainCtrl' };
$route.reload();

I know that $route.current can be updated because console.log after setting it shows that it does pickup the change:

console.log("current route: ", $route.current.templateUrl );

Unfortunately it does not work. The application I am creating have additional parameters defined using $routeProvider that I would like to preserve.

Any ideas?

Here is a Plunker I setup to illustrate this.

Community
  • 1
  • 1
marcoseu
  • 3,892
  • 2
  • 16
  • 35
  • I, of course, could be mistaken, but I don't think you can do this with `$route` (`$route.current` is read-only). If you need to save state across multiple controllers, consider using a service backed by in-memory data, cookies, localStorage, etc. – Michelle Tilley May 16 '13 at 08:17
  • maybe $location.absUrl() will help. Save its value, then you can log in and finally set the location to the stored value – Anton May 16 '13 at 08:20
  • @BrandonTilley that is the problem, $route.current is not read-only. If you add a `console.log("current route: ", $route.current.templateUrl );` after setting it, you will see that it has been updated. – marcoseu May 16 '13 at 10:13
  • @Anton unfortunately $location does not help as I set additional parameters as part of the route. For example, I have a parameter called 'auth' and was hoping that I can turn it on and off using code. There are certainly other way that I can accomplish that but this question is really about what I can do with $route. The answer might simple as what Brandon said: no, you can't. – marcoseu May 16 '13 at 10:22
  • Sorry, by read only, I meant that you are only meant to read it. The value is only there for you to inspect, not change. Being JavaScript in the browser, it's not literally read only. – Michelle Tilley May 16 '13 at 15:49
  • @BrandonTilley sorry, I should have been clearer as well. What I meant is that in the angular.js, all the magic related to $route seems to happen in `updateRoute` where it sets `last = $route.current`. I was hoping that by setting it, and confirming that it was set, would trigger necessary changes. An the answer is unfortunately no. I would have to dive into the detail of `updateRoute` to figure out the true impact. – marcoseu May 16 '13 at 16:17

0 Answers0