73

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';
   }
});
2
  • 2
    Is there a typo ==, it should be = Commented Sep 18, 2013 at 14:58
  • 1
    You could always use: window.location = 'google.com' Commented Sep 18, 2013 at 15:16

6 Answers 6

145

$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.

Sign up to request clarification or add additional context in comments.

4 Comments

nice touch with $window instead of window. this should be the accepted answer.
$window is the an extra module to be added to use the $window feature?
@GK123512648 no need, it is located in the core ns module as mentioned in the official API reference
I had to use $window.location = ..., without the .href.
10

If you want to change ng-view you'll have to use the '#'

$window.location.href= "#operation";

2 Comments

when I used this it generate the following error (although it worked): JavaScript runtime error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
or you can just call $location.path('/whatever/the/path/is')
6

The line

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

is wrong. First == makes a comparison, and what you are probably trying to do is an assignment, which is with simple = and not double ==.

And because absUrl() getter only. You can use url(), which can be used as a setter or as a getter if you want.

reference : http://docs.angularjs.org/api/ng.$location

2 Comments

I Didnt get using '='. Also in the documentation examples they have specified '=='
Won't work !!! absURL is READONLY. $location service provides getter methods for read-only parts of the URL (absUrl, protocol, host, port) and getter / setter methods for url, path, search, hash
6

It might help you!

AngularJs Code-sample

var app = angular.module('urlApp', []);
app.controller('urlCtrl', function ($scope, $log, $window) {
    $scope.ClickMeToRedirect = function () {
        var url = "http://" + $window.location.host + "/Account/Login";
        $log.log(url);
        $window.location.href = url;
    };
});

HTML Code-sample

<div ng-app="urlApp">
    <div ng-controller="urlCtrl">
        Redirect to <a href="#" ng-click="ClickMeToRedirect()">Click Me!</a>
    </div>
</div>

Comments

0

this worked for me inside a directive and works without refreshing the baseurl (just adds the endpoint).Good for Single Page Apps with routing mechanism.

  $(location).attr('href', 'http://localhost:10005/#/endpoint') 

Comments

-1

Try entering the url inside the function

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

1 Comment

This doesn't work (at least in Angular 1.2.8), it's simply appending http://www.google.com to the URL. $window.location.href seems to be working though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.