86

I'm trying to redirect to another route using:

$location.path("/route");

But for some reason it is not working. I did an auto-complete widget using jQuery-UI and I'm calling a function from the scope once the user selects an option. I debugged it and it enters the function but it is never redirected to the other route. It only changes the route when I press a key.

I think it is kind of strange but I haven't figured out how to solve this. I used

window.location = "#/route";

and it works but I want to use the path() function.

Does anybody have any idea why this is happening?

6 Answers 6

109

With an example of the not-working code, it will be easy to answer this question, but with this information the best that I can think is that you are calling the $location.path outside of the AngularJS digest.

Try doing this on the directive scope.$apply(function() { $location.path("/route"); });

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much and I'm sorry for not posting the not working example. But this was what my code needed.
Any idea how to get this to work in of the .success method inside of an $http request? @Tomarto
on firefox (30~) console fires an error that $apply cycle is busy
How is this supposed to work? I am getting the following error: Error: [$rootScope:inprog] $apply already in progress errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply at angular.js:78 ..... hence, it is not working as windows.location = ''..
@NicholasKreidberg had the same question and just adding $scope.$apply(); just after $location.path("/route"); worked for me.
81

Don't forget to inject $location into controller.

2 Comments

Nice catch - that was the problem for me.
Nice one, thank you for posting this, what seemed so obvious was not.
20

Assuming you're not using html5 routing, try $location.path("route"). This will redirect your browser to #/route which might be what you want.

Comments

15

If you need to redirect out of your angular application use $window.location. That was my case; hopefully someone will find it useful.

Comments

2

Check your routing method:

if your routing state is like this

 .state('app.register', {
    url: '/register',
    views: {
      'menuContent': {
        templateUrl: 'templates/register.html',
      }
    }
  }) 

then you should use

$location.path("/app/register");

Comments

0

It is hard to say without knowing your code. My best guess is that the onchange event is not firing when you change your textbox value from JavaScript code.

There are two ways for this to work; the first is to call onchange by yourself, and the second is to wait for the textbox to lose focus.

Check this question; same issue, different framework.

Comments

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.