changing $location.path() appears not to work in some circumstrances

15,872 views
Skip to first unread message

Mathias Verraes

unread,
Apr 5, 2012, 3:50:23 AM4/5/12
to ang...@googlegroups.com
Hi,

I'm trying to bind some actions to shortcut keys, using keymaster.js. 
However, for some reason, changing $location doesn't affect the url, when I'm inside a function passed to keymaster. Please see this example code:
(it's not in jsfiddle because you can't see url changes in jsfiddle anyway).

Am I doing something wrong, or is this a bug in either keymaster or angular?

thanks!

Mathias



Igor Minar

unread,
Apr 5, 2012, 3:47:14 PM4/5/12
to ang...@googlegroups.com
looks like the third-party code is executing the location update asynchronously - outside of the angular context, so you have to replace:

$location.path('/bar');

with:

$scope.$apply(function() {
$location.path('/bar');
});

/i




Mathias



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/idZpDp5x_uUJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

Mathias Verraes

unread,
Apr 6, 2012, 4:48:17 AM4/6/12
to ang...@googlegroups.com
Thanks, that did the trick :-)

Bastien Siebman

unread,
Mar 23, 2014, 6:39:38 AM3/23/14
to ang...@googlegroups.com
Thanks a lot!
 

Abhijeet Kamble

unread,
Mar 30, 2014, 9:50:16 AM3/30/14
to ang...@googlegroups.com
getting error as $apply already in progress....

Felix Mendez

unread,
Mar 31, 2014, 4:10:03 PM3/31/14
to ang...@googlegroups.com
try this:

$timeout(function () {
    $location.path('/bar');
});

don't forget to inject $timeout

Anusha Nilapu

unread,
Apr 17, 2015, 1:31:23 AM4/17/15
to ang...@googlegroups.com
I have same problem but $timeout solves my problem thanks alot
Reply all
Reply to author
Forward
0 new messages