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
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.
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.
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.
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.
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.
What about simple window.location.href = "http://google.com"
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With