Best way to programmatically change $routes?

8,470 views
Skip to first unread message

Ben Sgroi

unread,
Aug 24, 2012, 3:29:04 PM8/24/12
to ang...@googlegroups.com
What I'm trying to do is create a link that, when clicked, would call some action on the controller, and then change to a different route.  I can't use the $route events ($routeChangeStart or $routeChangeSuccess) because I don't want the action to occur every time the route is changed, only when the link is clicked.  So far, the only way I can think of to do this is to iterate through $route.routes and find the correct route, then assign it to $route.current, but that really seems like a hack.  There has to be a better way to do this.  Anyone have any ideas?

Thanks.


Pawel Kozlowski

unread,
Aug 24, 2012, 3:32:09 PM8/24/12
to ang...@googlegroups.com
Hi!
Yes, use the $location service (http://docs.angularjs.org/api/ng.$location).
You could call $location.path('/my/path') to navigate to a new route
(setting query params is also possible using $location).

Hope this helps,
Pawel

Ben Sgroi

unread,
Aug 24, 2012, 3:39:01 PM8/24/12
to ang...@googlegroups.com
Hi Pawel - thanks for your fast response!  Setting $location.path was actually the first thing I tried, and the results were weird: it would flash to the new route but immediately switch back to the previous one (and updating the URL accordingly).  I can try to put together a jsfiddle to demonstrate.

Pawel Kozlowski

unread,
Aug 24, 2012, 3:42:24 PM8/24/12
to ang...@googlegroups.com
hi!

On Fri, Aug 24, 2012 at 9:39 PM, Ben Sgroi <bsg...@gmail.com> wrote:
> Hi Pawel - thanks for your fast response! Setting $location.path was
> actually the first thing I tried, and the results were weird: it would flash
> to the new route but immediately switch back to the previous one (and
> updating the URL accordingly). I can try to put together a jsfiddle to
> demonstrate.

yep, having a jsFiddle would help to help :-)
I still claim that $location is the way to go but maybe there is
something wired doing on... Will see with the code example!

Cheers,
Pawel

Ben Sgroi

unread,
Aug 24, 2012, 3:59:21 PM8/24/12
to ang...@googlegroups.com
*sigh*   Well of course it works fine in the jsfiddle.  There must be something else going on in my app.  

Here's the jsfiddle, for the curious:  http://jsfiddle.net/bensgroi/RK4bK/

Thanks for your help!

Johan

unread,
Aug 25, 2012, 12:25:39 AM8/25/12
to ang...@googlegroups.com
Using $location inside a controller  can give strange results - usually you'll find if you try the same route using a <a href="...">my link</a> it will work fine.

Of course using a link does not help if you want to disable the link or add controller logic via a function call.

$location does not do a full page reload: http://docs.angularjs.org/guide/dev_guide.services.$location

You can try using the lower level API as mentioned above: $window.location.href

Witold Szczerba

unread,
Aug 26, 2012, 11:14:02 AM8/26/12
to ang...@googlegroups.com
Hi,
What kind of strange result can one get when using $location inside a
controller? Where is the right place to use $location instead?
You said the $location does not do a full page reload, but why would
anyone like to have a full page reload? It is like restarting an
application when going from one view to the other, isn't it?

Regards,
Witold Szczerba
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>

Will Kriski

unread,
Aug 26, 2012, 12:52:44 PM8/26/12
to ang...@googlegroups.com
I assume if you're providing download links (the original question) you wouldn't want to use $location as that is used for routing single page apps (routing to new views). So using <a href={{link}}>Download</a> should work or via javascript as suggested above.

Will
Reply all
Reply to author
Forward
0 new messages