96

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.

4
  • Should probably add that there is server side authentication on the data as well, so the above is not the only authentication, it is more a matter of convenience to redirect to the login-page, instead of showing a mostly empty page. Commented Jul 17, 2014 at 2:56
  • 1
    you need the native location object using $window.location=... Commented Jul 17, 2014 at 3:20
  • INstead you could consider to make it all a AngularJS including your authentication -- see this post frederiknakstad.com/2013/01/21/… Commented Jul 17, 2014 at 3:23
  • The plan is to include everything (including login) inside the AngularJS app eventually, but as we are currently making the transition to AngularJS, the registration/login screen seems to be the least important at this stage. Commented Jul 17, 2014 at 3:36

5 Answers 5

86

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

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.example/login"
Sign up to request clarification or add additional context in comments.

9 Comments

Just tried that. Unfortunately, that redirects to www.domain.com/app/RouteTo/login rather than www.domain.com/login
Right, that wasn't mean to be literal. I don't know what you've defined as the route for your login view. Place whatever that route might be in the the argument for the $location.url() method. I've edited the answer for clarity.
Ah right. The issue here is that $location.url still redirects to a sub-path of the app. So using $location.url('/login') redirects to www.domain.com/app/login rather than www.domain.com/login
Well, as stated in the question, currently the login page sits outside the AngularJS app. Hence the need to redirect to www.domain.com/login rather than a path within the app.
It's also worth mentioning you should probably use $window instead of window (source: stackoverflow.com/questions/28906180/…)
|
42

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

Comments

20

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>

Comments

4

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.

Comments

-11

You have to put:

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

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

1 Comment

Don't be afraid!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.