Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect using AngularJS

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?

like image 310
Tomarto Avatar asked Aug 10 '12 19:08

Tomarto


People also ask

How to redirect to another page in AngularJS on button click?

Redirect to another page in AngularJS Here, the div element acts as the AngularJS app as well as the controller. The ng-click directive tells what to do when the button(here) is pressed. In this case, it calls the redirect() method.

What is $location in AngularJS?

The $location in AngularJS basically uses a window. location service. The $location is used to read or change the URL in the browser and it is used to reflect that URL on our page. Any change made in the URL is stored in the $location service in AngularJS.


2 Answers

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

like image 97
Renan Tomal Fernandes Avatar answered Oct 09 '22 20:10

Renan Tomal Fernandes


Don't forget to inject $location into controller.

like image 43
Stepan Suvorov Avatar answered Oct 09 '22 22:10

Stepan Suvorov