Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Using angular ui-router I'm trying to make nested routing based on condition.i.e Want to check a condition before loading a state.

    .state('main.home', {
        url: "/:cnt",
        abstract: true,
        templateUrl: function ($stateParams) {
            return template1;
        },
        controller: "myController",
        resolve: {
            //Some model
            }],

            lazy: ['$ocLazyLoad', '$stateParams', function ($ocLazyLoad, $stateParams) {
               //lazily loaded controllers
            }]
        },
        onEnter: updateAppValues,
    }).state('main.home.default', {
        url: '',
        templateUrl: function ($stateParams) {
            return template2;
        },
        resolve: {
            lazy: ['$ocLazyLoad', function ($ocLazyLoad) {
                //lazily loaded controllers
            }]
        },
        controller: 'myDefaultController',
    })

basically the nested router main.home.default must be loaded conditionally

if(something){
    //loaded state main.home.default
}

How can I achieve this?

share|improve this question
    
you can write a if condition in resolve and in negative case use $location path to redirect to other route – Vinod Louis 2 days ago
    
Did you check if my answer solved your issue? :) – Mistalis 15 hours ago
app.run(function($rootScope, $state) {
   $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) { 
     if (toState.name == 'login'){ //if the name of that state is login
    //go to login page
      }
   });
});

Does this give you any idea? or

app.run(function($rootScope, $state){
   $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams) {
      if(*your condition*) {
         $state.transitTo('stateYouWantToGo')
         event.preventDefault()
       }
    });
 });
share|improve this answer

You can catch the event of route changing with:

$rootScope.$on("$routeChangeStart", function(event, next, current) {
    if(next.$$route == 'routeYouWantToAvoid') { // Or whatever you want
        $state.transitTo('main.home.default');
    }
});
  • next is the route you are going to.

  • current the route you are coming from.

Here is the documentation if you need more information.

share|improve this answer

You can add if condition in the resolve of main.home.default. ex:

 resolve:{
    "check":function($location){   
        if('Your Condition'){ 
            //Do something
        }else{
            $location.path('/');    //redirect user to home.
            alert("You dont belong here");
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.