I am testing with the following AngularJS $location. I don't what's the problem with this. Just want to check if the redirection is working or not:
HTML
<body data-ng-controller="MainCtrl"> Hello {{name}}! <button ng-click='go()'>Go</button> </body>
AngularJS code
var app = angular.module('location', []); app.controller('MainCtrl', function($scope,$routeParams, $location) { $scope.name = 'World'; $scope.go = function() { $location.absUrl() = 'http://www.google.com'; } });
Use the $location Service in AngularJS to Redirect to Another Page. The $location service is used to get the URL of the current page and change it. It can also set up a route and redirect to a new page.
You can use $location. path('/newPageUrl') to navigate to new page.
To change path URL with AngularJS, $location. path() is passed the new URL as a parameter, and the page is automatically reloaded. This means that if you ever make a change to the URL in an Angular application, then the partial and controller will be reloaded automatically.
Overview. The $location service parses the URL in the browser address bar (based on the window. location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.
$location won't help you with external URLs, use the $window service instead:
$window.location.href = 'http://www.google.com';
Note that you could use the window object, but it is bad practice since $window is easily mockable whereas window is not.
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