4

I want to redirect mypage to home page if sessionStorage.logged is set to true. Below is my code. why my below code is not working.

Since i am new to angularjs i dont know how to this. I will be more thankfull to u if u help to solve this

.when('/login', 
{
    templateUrl: 'views/login.html',
    controller:'LoginCtrl',
    resolve:
    {
            mess:function()
            {
                var t=(sessionStorage.logged).toString();
                if(t=="true")
                {
                    redirectTo: '/home';
                }
            }

    }
})
bharath
  • 845
  • 2
  • 11
  • 23
  • 2
    Not really an answer, but a workaround: instead of the 'resolve', add in your logincontroller: if(alreadyLoggedin){$location.path('/home');} – Fortega Sep 04 '14 at 11:08
  • 1
    @Fortega - The controller logic only executes the first time the controller is loaded. If the user has anyway of manually hitting /login a second time, your solution will not execute. – andyzinsser Jan 06 '15 at 00:01

1 Answers1

6

Use $location.path(), it works just like redirectTo:

.when('/login', 
{
    templateUrl: 'views/login.html',
    controller:'LoginCtrl',
    resolve:
    {
            mess:function($location)
            {
                var t=(sessionStorage.logged).toString();
                if(t=="true")
                {
                    $location.path('/home');
                    //redirectTo: '/home';
                }
            }

    }
})
khemry
  • 585
  • 3
  • 7
  • 1
    Another option I found is the onEnter event: http://stackoverflow.com/questions/25047884/angular-ui-router-to-accomplish-a-conditional-view – andyzinsser Jan 06 '15 at 00:02