0

it's probably a rookie mistake but I am having trouble changing my page using:

$state.go('login');

My routing looks as following:

$stateProvider
   .state('login', {
       url: "/login",
       templateUrl: "./auth/login.html",
       controller: 'loginCtrl'
   })

I also added a

$rootScope.$on("$stateChangeStart", function (event, next) {...}

just to check if it gets there after I use $state.go() and yes; it gets there but it does not change my page or URL at all. Changing the page works fine with ui-sref="login" inside a view.html.

Other syntaxes I tried:

$state.go('login', null, {options: { location : true }  } );
$state.go('login', {}, {location : true});

Btw, what I am trying to accomplish is redirecting to a login-page when the user is not authorized inside the $stageChangeStart-event.

Any ideas?

gerb0n
  • 380
  • 1
  • 5
  • 19
  • `$stateChangeStart` gets fired at the **start** of the statechange and doesn't necessarily mean it succeeds. `$stateChangeSuccess` is a better test. Are there any errors in the console? – Ed_ Jul 27 '15 at 15:22
  • There are no errors in the console. As soon as $state.go('login') is fired it immediately gets inside the **$stateChangeStart**. When it's done I get inside **stateChangeSuccess**. Do I need **event.preventDefault()** in order to change page/url? – gerb0n Jul 27 '15 at 15:31
  • No, `event.preventDefault()` would stop the state from changing. I am not sure what the problem is if there are no errors and it's working with `ui-sref`... very confusing. The only thing that is new to me in your code is using `./auth/login.html` rather than just `auth/login.html` that I would use, but I doubt that will make any difference. – Ed_ Jul 27 '15 at 16:44
  • One thing you should check - put a `console.log` statement in `loginCtrl` and see if it's being run? If it is, check the network tab of your dev tools and ensure the `GET` request for `login.html` is succeeding... – Ed_ Jul 27 '15 at 16:45

1 Answers1

0

Visit Angular ui router - Redirection doesn't work at all . It gave me a more clear idea of what I was doing wrong.

The actual solution for me was found at the following page answered by frankwallis:

https://github.com/angular-ui/ui-router/issues/600

I replaced my default $urlRouterProvider.otherwise('/yourpage'); with

$urlRouterProvider.otherwise( function($injector, $location) {
     var $state = $injector.get("$state");
     $state.go("app.home");
});

Too bad I don't really understand the solution but atleast it works now!

Community
  • 1
  • 1
gerb0n
  • 380
  • 1
  • 5
  • 19