Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect to an absolute url in angularjs

Tags:

How to redirect user to an absolute url in angularjs.

For example: i have a page, my current url (http://example.com/submit), once the user click some button in the web page, he has to get redirected to some other web page for example ('http://google.com').

I tried using

$location.absUrl() == 'http://www.google.com' $location.path('http://www.google.com') $location.url('http://www.google.com') 

But nothing works.

Note: The rediretion should not occur from the web-server. Its an REST API. Wanted to achieve in client side(using angularjs) if the user is valid user(authenticated user), redirected him to portal)

Thanks

like image 577
swastican Avatar asked Jan 12 '14 10:01

swastican


People also ask

How to change URL path in AngularJS?

AngularJs Change Url Example:$location. url('/User/'+userId); will set the current url to the new url example – www.example.com/User/98. You can call the function – changeUrl(userId) on button click. Note : Make Sure the service $location is imported before using it.

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.

What is $window in AngularJS?

A reference to the browser's window object. While window is globally available in JavaScript, it causes testability problems, because it is a global variable. In AngularJS we always refer to it through the $window service, so it may be overridden, removed or mocked for testing.

What is html5Mode?

In HTML5 mode, the $location service getters and setters interact with the browser URL address through the HTML5 history API. This allows for use of regular URL path and search segments, instead of their hashbang equivalents.


2 Answers

You need to use the browser level API, with the object window, or you can inject the $window reference in your controller.

  • window.location.href="http://www.google.com";
  • $window.location.href="http://www.google.com";

References:

window

$window

Best solution should be perform redirect in your $routeProvider configuration, injecting the $window object and performing all redirection you need.

like image 92
StarsSky Avatar answered Oct 07 '22 00:10

StarsSky


What about simple window.location.href = "http://google.com"?

like image 21
methyl Avatar answered Oct 06 '22 23:10

methyl