2

On first page load when app gets initialized I want to redirect the user to login page. I think the relevant part of the code is this

$rootScope.$on("$routeChangeStart", function (event, next, current) {
    alert("change location");

    $location.path('/login');
});

It is based on https://github.com/fnakstad/angular-client-side-auth/blob/master/app/js/app.js The problem is on page load the alert is triggered but location does not change. I have to click on a navigation item of my app and then the login action will be called and the route changes.

.run(['$rootScope', '$location', '$cookieStore', function ($rootScope,
                                                     $location, $cookieStore) {

    $location.path('/login');

    $rootScope.$on("$routeChangeStart", function (event, next, current) {

        $location.path('/login');

    });

    $rootScope.appInitialized = true;
}]);

This will work however seems redundant. And why is alert triggered but not location change?

Why does the location not changes on full page load? How to fix this?

Full code http://jsfiddle.net/qfSC3/ but fiddle does not work.

1 Answer 1

3

Try using the $locationChangeStart event instead

$scope.$on("$locationChangeStart", function(event){
    event.preventDefault();
})

Based off this question: AngularJS - Detecting, stalling, and cancelling route changes

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.