Redirection with AngularJS

I am trying to redirect to another route using:

$location.path("/route"); 

But for some reason it does not work. I made an autocomplete widget using jQuery-UI and I call a function from the scope as soon as the user selects a parameter. I debugged it and it enters the function but never redirects to another route. It changes the route only when a key is pressed.

I think this is strange, but I did not understand how to solve this. I used

 window.location = "#/route"; 

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

Does anyone know why this is happening?

+86
redirect angularjs location
Aug 10 '12 at 19:21
source share
6 answers

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

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

+108
Aug 10 2018-12-12T00:
source share

Remember to enter $location in the controller.

+82
Dec 07
source share

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

+20
Aug 10 2018-12-12T00:
source share

If you need to redirect from an angular application, use $window.location . That was my business; hope someone finds this helpful.

+14
Jan 29 '15 at 14:34
source share

Check your routing method:

if your routing state looks like this

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

then you should use

 $location.path("/app/register"); 
+2
Jul 11 '16 at 20:47
source share

It's hard to say without knowing your code. It’s best to assume that the onchange event onchange not fire when the value of your text field changes from JavaScript code.

There are two ways to do this: the first is to call onchange yourself, and the second is to wait until the text field loses focus.

Mark this question ; same problem, different scope.

0
Aug 11 2018-12-12T00:
source share



All Articles