1

I have a problem with angularjs url redirect. Using $location.path() to load the url seems working perfectly with url pattern likes $location.path('/admin'). But when i tried to load $location.path('/admin/home') it didn't work. If i tried with $location.path('home'), the html template is loaded. Below is my code.

  $stateProvider
        .state('admin', {
            url: '/admin',
            templateUrl: 'Admin/admin.html',
            controller: 'adminController',
            controllerAs: 'vm'
        })
        .state('admin.home', {
            url: '/admin/home',
            templateUrl: 'Admin/admin.home.html'
            controller: 'adminHomeController',
            controllerAs: 'vm'
        })

Controller script

authorizeUser(function(result){
     if(result === true) {
         $location.path('/admin/home');
     }
}

2 Answers 2

2

As you are using nested state, don't use $location.path here. Instead, inject $state and use $state.go with the state name as below.

authorizeUser(function(result){
     if(result === true) {
         $state.go('admin.home');
     }
}

Note: As this (admin.home) is a nested state of admin state, you should have another ui-view inside the template Admin/admin.html of admin state.

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

2 Comments

Thanks a lot man @aruna. You just saved my day. It works like a charm. I just forgot to put ui-view inside the template.
@CodeRedz Great and can you accept the answer then :-) ?
0

1. $state.go() by state id

$state.go('admin.home');

2. $location.path() with url path.

In my experience, "$location.path()" function has a very slight delay to call.

so, you have to use with $timeout service.

authorizeUser(function(result){
     if(result === true) {
        $timeout(function() {
          $location.path('/admin/home');
        }, 300);
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.