1

I know we can reload the current page in angularjs using $window.location.reload();.

How can we reload to a different location, something like -

$window.location.reload('/anotherUrl');

Cœur
  • 32,421
  • 21
  • 173
  • 232
dafriskymonkey
  • 1,959
  • 5
  • 21
  • 45
  • it's not `angular` - it's `javascript`, which `angular` is using. your answer: http://stackoverflow.com/questions/8824141/how-to-redirect-from-one-url-to-another-url – rzysia Jan 14 '15 at 13:54

3 Answers3

1

You are trying to redirect to different location, for that you can use window services like this

$window.location.href = 'anotherUrl' ;
andlrc
  • 41,466
  • 13
  • 82
  • 108
ssilas777
  • 9,166
  • 3
  • 41
  • 63
1

I think what your looking for is to use $location service.

Specifically, you can set a new url with this:

$location.path('/anotherUrl');
andlrc
  • 41,466
  • 13
  • 82
  • 108
mfink
  • 972
  • 16
  • 29
1

If you want to use AngularJS to change the view from code and add the change to history do this:

$location.url('/anotherUrl');

(if you want to lose current page's search (query params) and hash),

or

$location.path('/anotherurl');

(if you want to pass current url's search (query params) and hash to the new page)

If you don't want to add this change to history do this:

$location.replace();
$location.url('/anotherUrl');

or

$location.replace();
$location.path('/anotherurl');

$location.replace(); will maintain your state without re-instantiating your scope or controller. If you truly do want to reload the page (and thereby lose your state) you could fall back to the pure JavaScript solution given in the comment made by @null, that you have requested be given as an answer, or use the method I outline above and then call:

$route.reload();

Please see this link for replace(); documentation.

I have assumed you are using routing since your question is asking how to do this using AngularJS.

lukkea
  • 3,526
  • 2
  • 35
  • 49