1

I am trying to use resolve and conditional routing in angular ui-router but it doesnt seem to work

App.js:

app.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

    .state('app', {
    url: '/app',
    abstract: true,
    templateUrl: 'templates/home.html'
  })


  .state('app.login', {
    url: '/login',
    views: {
      'menuContent': {
        templateUrl: 'templates/login.html'
      }
    }
  })

  .state('app.therapist', {
    url: '/therapist',
    views: {
      'menuContent': {
        templateUrl: 'templates/therapist.html'
      }
    }


  }).state('app.trial', {
    url: '/trial',
    views: {
      'menuContent': {
        templateUrl: 'templates/trial.html'
      }
    },
    resolve:{
      check:function($state){
        if(1==1){
           e.preventDefault();
          $state.go('app.login',{}) ;  /**/Here trying**
        }else{
          return true;
        }
      }
    }
$urlRouterProvider.otherwise('/app/therapist');

  });

So in app.trail route I was testing and trying to redirect to app.login but it doesnt work it only opens the trial page

1 Answer 1

1

In e.preventDefault(); you appear to be missing anything defined as e.

You can use preventDefault() when handling an event such as $stateChangeStart, but I don't think that is available within resolve.

See Angular ui-router $state.go is not redirecting inside resolve for some suggestions on how to handle this (do the check inside a handler for $stateChangeStart, or send an event to trigger the transition, or use a timeout to delay your state change until after the first one has completed).

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

1 Comment

I read in another question to include this but anyways how would I make this work ?I am relatively a beginner.Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.