Using $window or $location to Redirect in AngularJS


The app I am working on contains various states (using ui-router), where some states require you to be logged in, others are publicly available.

I have created a method that validly checks whether a user is logged in, what I am currently having issues with is actually redirecting to our login-page when necessary. It should be noted that the login page is not currently placed within the AngularJS app. ($rootScope, $location, $window) {

    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {

        if ( && !$ {
            var landingUrl = $ + "/login";
            $, "_self");

The console.log shows the intended url properly. The line after that, I have tried practically everything from $ to window.location.href and no matter what I’ve tried no redirect happens.


Found the issue.

var landingUrl = $ + "/login";
$, "_self");

The variable landingUrl was set to ‘’, which would not work with
$window.location.href (which was one of the things I tried). However after changing the code to

var landingUrl = "http://" + $ + "/login";
$window.location.href = landingUrl;

it now works.


I believe the way to do this is $location.url('/RouteTo/Login');

Edit for Clarity

Say my route for my login view was /Login, I would say $location.url('/Login') to navigate to that route.

For locations outside of the Angular app (i.e. no route defined), plain old JavaScript will serve:

window.location = ""

Answered By – m.casey

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.