Search code examples
angularjsstate.go

AngularJS $state.go is not redirecting to other page


Below is my controller code,

app.controller('logoutCtrl', ['$scope', '$http','$window','$state',
  function ($scope, $http,$window,$state) {

        $scope.logout = function() {
          console.log('inside logmeout');
          delete $window.sessionStorage.token;
          $state.go('access.login');
        };

  }]);

HTML

<li class="last" ng-controller="logoutCtrl">
    <a href="" ng-click="logout()">
         <i class="material-icons">lock</i> Logout
    </a>
</li>

app.router.js

            .state('access', {
                url: '/access',
                template: '<div ui-view class=""></div>'
            })
            .state('access.login', {
                url: '/login',
                templateUrl: 'partials/ui-login.html',
                controller: 'LoginFormController',
                resolve: {
                    deps: ['uiLoad',
                        function(uiLoad) {
                            return uiLoad.load(['scripts/controllers/login.js',
                                '../bower_components/font-awesome/css/font-awesome.css']);
                        }
                    ]
                }
            })

When clicking the logout i am not able to redirect to another state('access.login'). The control is coming inside the logout() and able to print the console message and is deleting the token as well but redirect not happening.. Can i get any help..


Solution

  • In your module definition you need to pass 'ui.router' as a dependency in order to use the Angular-UI-Router in your project:

    E.g. angular.module('my_app', ['ionic', 'ui.router'])

    It works for me as well.

    $state.go('the-state-name-in-quotes')