重新加载浏览器时AngularJs ui-router当前状态为空(按F5键)

时间:2016-11-18 07:34:37

标签: javascript angularjs angular-ui-router

我的问题是,当我在浏览器地址栏中输入路线或按F5按钮时,我总是重定向到“工作区”,因为$ state.current总是空着这样:

 { name: "", url: "^", views: null, abstract: true }

这是我的app.run方法

 var dependencies = ['smart-table', 'angular-loading-bar', 'ui.router',....]
 var app = ng.module('myApp', dependencies);  
 app
    .run(['$rootScope', '$state', '$stateParams', '$location',
        'nc.bpt.authentication.services.authenticationService', 'modularity.services.widgetService',
        function ($rootScope, $state, $stateParams, $location, authenticationService, mainMenuService) {

            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;

            $rootScope.$on('unauthorized', function (event, rejection) {
                $state.go('login');
            });

            $rootScope.$on('loginStatusChanged', function (event, authContext) {
                if (authContext.isAuthenticated) {

                    mainMenuService.build(authContext.roles);


                    if (!$state.current.name || $state.current.name === 'login') {
                        $state.go('workspace');
                    }

                } else {
                    $state.go('login');
                }
            });

            authenticationService.fillAuthData();
        }]);

现在主控制器:

ng.module('myApp.controllers')
    .controller('myApp.controllers.appController', ['$scope', '$state','$urlRouter',
        function ($scope, $state, $urlRouter) {
            $scope.authenticated = false;
            $scope.currentState = $state.current.name;
            pushNotificationService.run();

            $scope.$on('unauthorized', function (event, rejection) {
                $scope.authenticated = false;
            });

            $scope.$on('loginStatusChanged', function (event, authContext) {
                $scope.authenticated = authContext.isAuthenticated;
            });

            $scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
                $scope.currentState = toState.name;
                $urlRouter.sync();
            });
        }]);

现在是国家:

ng.module('myApp.tenancy')
    .config(['$stateProvider',
        function ($stateProvider) {

            $stateProvider
                .state('tenants', {
                    abstract: true,
                    url: 'administration/tenants',
                    template: '<ui-view/>'                   
                })
                .state('tenants.list', {
                    url: '/',
                    templateUrl: '$(Platform)/Scripts/Components/tenancy/views/tenant-list.html'
                })
                .state('tenants.details', {
                    url: '/:id',
                    templateUrl: '$(Platform)/Scripts/Components/tenancy/views/tenant-detail.html'
                })
                .state('tenants.edit', {
                    url: '/:id',
                    templateUrl: '$(Platform)/Scripts/Components/tenancy/views/tenant-edit.html'
                })
                .state('tenants.create', {
                    url: '/create',
                    templateUrl: '$(Platform)/Scripts/Components/tenancy/views/tenant-create.html'
                })
        }]);

0 个答案:

没有答案