0

Here is my route config:

$stateProvider
      .state('login', {
        url: '/',
        templateUrl: function(){
          return 'app/login/login.html';
        },
        controller: 'loginCtrl as ctrl',
        data: {
          requireLogin: false
        }
      })
      .state('loginSerial', {
        url: '/login/createCase?sn=',
        templateUrl: function(){
          return 'app/login/login.html';
        },
        controller: 'loginCtrl as ctrl',
        data: {
          requireLogin: false
        }
      })

on $stateChangeStart - I am redirecting as :

if( !$rootScope.isUserLoggedIn && !$rootScope.serialLogin ){

        console.log('no one is logged in!', toParams.sn);
        $state.go('/login/createCase',{sn:toParams.sn});;
        event.preventDefault();
        return;

      } 

It works fine. But I am getting following error :

angular.js:11706 Error: Could not resolve '/login/createCase' from state ''
    at Object.transitionTo (angular-ui-router.js:3179)
    at Object.go (angular-ui-router.js:3107)
    at index.route.js:152
    at Scope.$broadcast (angular.js:14889)
    at Object.transitionTo (angular-ui-router.js:3272)
    at Array.<anonymous> (angular-ui-router.js:2383)
    at Object.invoke (angular.js:4219)
    at handleIfMatch (angular-ui-router.js:1868)
    at angular-ui-router.js:1925
    at check (angular-ui-router.js:2041)

How to solve this? and what is wrong with my side? any one help me?

user2024080
  • 1
  • 14
  • 56
  • 96

2 Answers2

1

You should pass state name, not state's url

// not this
$state.go('/login/createCase',{sn:toParams.sn});
// but that
$state.go('loginSerial',{sn:toParams.sn});

Extend, also, before any redirection, I would strongly suggest to check, if we are not already being redirected:

if(toState.name === 'loginSerial'){
    return; // do not continue in redirection, if already redirected
}
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Ready to assist, but you should show more.. html template.. and even better... some plunker. There are some similar Q & A with plunkers http://stackoverflow.com/q/26135382/1679310 – Radim Köhler Oct 21 '16 at 12:44
  • As a test can I update my url and test like this: `http://localhost:3000/login/createCase?sn=3000` - – user2024080 Oct 21 '16 at 12:48
  • Well, are you sure, that current state (before redirection) also has sn param? I doubt... that every has that.. so you should do `$state.go('loginSerial',{sn:toParams.sn || 3000});` – Radim Köhler Oct 21 '16 at 12:50
  • Well, there are parts still missing, e.g. login controller. We should do the check if use was redirected very soon. For example this `if( !$rootScope.isUserLoggedIn && !$rootScope.serialLogin && toState.name === 'loginSerial' )` should be `if( !$rootScope.isUserLoggedIn && !$rootScope.serialLogin && toState.name !== 'loginSerial' )` - otherwise it will create a lop - here is a really working handling, pretty similar to your issue.. try to use that snippet http://stackoverflow.com/a/25029259/1679310 – Radim Köhler Oct 21 '16 at 13:06
0

$state.go requires that you pass in a state name and not a state url. So you'll need to execute

$state.go('loginSerial', {params});
John F.
  • 4,780
  • 5
  • 28
  • 40
  • Yes, Now the error gone. But page not displayed, But before I got login page displayed with error. anything need to go further fine tune? – user2024080 Oct 21 '16 at 12:43
  • @user2024080 remove the `=` in your url for your state `/login/createCase?sn=` so that it's `/login/createCase?sn`. This is based on the docs https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters – John F. Oct 21 '16 at 12:45