2016-01-22 105 views
2

我在项目中使用angular的UI路由器和嵌套路由,我面临的问题是,当我使用链接(ui-sref)时,使用userId作为url的一部分导航到用户的详细信息页面。当我刷新页面时,状态参数丢失。StateParams在页面刷新时使用UI路由器是空的

所以我看了一下角度演示:http://angular-ui.github.io/ui-router/sample/#/contacts/1/item/b并且不能重现这种行为,但是嵌套状态不是这个演示的一部分,与我的应用程序相反。

$stateProvider 
    .state('base', { 
     abstract: true 
    }) 
    .state('home', { 
     parent: 'base', 
     url: '/', 
     views: { 
     '[email protected]': { 
      templateUrl: 'app/index/index.view.html', 
      controller: 'IndexController', 
      controllerAs: 'home' 
     } 
     } 
    }) 
    .state('users', { 
     url: '/users', 
     parent: 'base', 
     abstract: true 
    }) 
    .state('users.list', { 
     url: '/list', 
     views: { 
     '[email protected]': { 
      templateUrl: 'app/users/users.list.html', 
      controller: 'UsersController', 
      controllerAs: 'users' 
     } 
     }, 
     permissions: { 
     authorizedRoles: UserRoles.ALL_ROLES 
     } 
    }) 
    .state('users.details', { 
     url: '/:userId/details', 
     views: { 
     '[email protected]': { 
      templateUrl: 'app/users/user.details.html', 
      controller: 'UserDetailsController', 
      controllerAs: 'userDetails' 
     } 
     }, 
     resolve: { 
     logSomeParams: ['$stateParams', '$state', function ($stateParams, $state) { 
      console.log($stateParams); 
      console.log(this); 
      console.log($state); 
     }] 
     } 
    }) 

当刷新页面的URL立即变为http://localhost:3000/#/users//details和控制台输出(解析功能)显示,PARAMS丢失。

html5Mode(LocationProvider)未启用。如果userId在页面刷新时缺失,我已经找到“解决方案”像重定向回列表,但我无法相信没有更好的方法来解决这个问题。

这是我怎么在概述链接的详细信息页面(以及它的工作):

<div class="panel-body" ui-sref="users.details({userId: user.siloUserId})"> 

回答

2

正如预期的那样我的问题,帽子无关与UI路由器。即使您刷新页面,URLMatcher也能按预期工作(其他所有内容都将是一个巨大的失望)。

但是,我有一个$ stateChangeStart侦听器,用于检查SAML身份验证会话(cookie)是否仍然有效并等待结果。

function(event, toState, toParams, fromState, fromParams){ 
    if(!authenticationService.isInitialized()) { 
     event.preventDefault(); 
     authenticationService.getDeferred().then(() => { 
     $state.go(toState); 
     }); 
    } else { 
     //check authorization 
     if (authenticationService.protectedState(toState) && !authenticationService.isAuthorized(toState.permissions.authorizedRoles)) { 
     authenticationService.denyAccess(); 
     event.preventDefault(); 
     } 
    } 
} 

不知道如何发生这种情况,但我忘了将参数传递给stateProvider的go方法。所以我必须改变的是添加参数:

$state.go(toState, toParams);