Using $window or $location to Redirect in AngularJS

JavascriptAngularjsRedirectAngular Ui-Router

Javascript Problem Overview


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.

app.run(function ($rootScope, $location, $window) {
    

    $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
        
        if (toState.data.loginReq && !$rootScope.me.loggedIn) {
            var landingUrl = $window.location.host + "/login";
            console.log(landingUrl);
            $window.open(landingUrl, "_self");
        }
    });
});

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

EDIT (RESOLVED):

Found the issue.

var landingUrl = $window.location.host + "/login";
$window.open(landingUrl, "_self");

The variable landingUrl was set to 'domain.com/login', 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://" + $window.location.host + "/login";
$window.location.href = landingUrl;

it now works.

Javascript Solutions


Solution 1 - Javascript

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 = "http://www.my-domain.com/login"

Solution 2 - Javascript

It seems that for full page reload $window.location.href is the preferred way.

> It does not cause a full page reload when the browser URL is changed. To reload the page after changing the URL, use the lower-level API, $window.location.href.

https://docs.angularjs.org/guide/$location

Solution 3 - Javascript

It might help you! demo

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>

Solution 4 - Javascript

Not sure from what version, but I use 1.3.14 and you can just use:

window.location.href = '/employee/1';

No need to inject $location or $window in the controller and no need to get the current host address.

Solution 5 - Javascript

You have to put:

<html ng-app="urlApp" ng-controller="urlCtrl">

This way the angular function can access into "window" object

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThorbj&#248;rn Kappel HansenView Question on Stackoverflow
Solution 1 - Javascriptm.caseyView Answer on Stackoverflow
Solution 2 - JavascriptDiego FerriView Answer on Stackoverflow
Solution 3 - JavascriptAnil SinghView Answer on Stackoverflow
Solution 4 - JavascriptCularBytesView Answer on Stackoverflow
Solution 5 - JavascriptEfrain Luna VillafuerteView Answer on Stackoverflow