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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Tomarto
  • 2,755
  • 6
  • 27
  • 37

6 Answers6

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"); });

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Renan Tomal Fernandes
  • 10,978
  • 4
  • 48
  • 31
  • Thank you very much and I'm sorry for not posting the not working example. But this was what my code needed. – Tomarto Aug 14 '12 at 17:09
  • 23
    Any idea how to get this to work in of the .success method inside of an $http request? @Tomarto – niczak Oct 19 '13 at 23:24
  • 2
    on firefox (30~) console fires an error that $apply cycle is busy – svarog Jan 28 '15 at 20:11
  • 1
    How is this supposed to work? I am getting the following error: Error: [$rootScope:inprog] $apply already in progress http://errors.angularjs.org/1.2.15/$rootScope/inprog?p0=%24apply at angular.js:78 ..... hence, it is not working as windows.location = ''.. – Vaibhav Jul 15 '15 at 08:44
  • 1
    @NicholasKreidberg had the same question and just adding $scope.$apply(); just after $location.path("/route"); worked for me. – Ernesto Allely Jun 07 '16 at 09:44
81

Don't forget to inject $location into controller.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
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.

Patrick
  • 6,495
  • 6
  • 51
  • 78
dpactri
  • 251
  • 2
  • 8
15

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

Emperor XLII
  • 13,014
  • 11
  • 65
  • 75
Raulucco
  • 3,406
  • 1
  • 21
  • 27
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");
jfindley
  • 682
  • 7
  • 6
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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Milan Jaric
  • 5,556
  • 2
  • 26
  • 34