【发布时间】:2013-12-09 21:48:05
【问题描述】:
在我的应用中,我有一个名为“仪表板”的状态,其中包含多个子状态
.state('dashboard', {
url: '/dashboard',
abstract: true,
// resolve async objects before controller instantiation.
resolve: {
productResolve: function ($state, authService, cacheService, productService) {
var cache = cacheService.getCache();
if (cache.length === 0){
return productService.getProductsAndCommoditiesAsync(authService.getCredentials().roleID)
.then(function(productData){
cacheService.setCache(productData);
},
function(errorMessage){
$state.go('error',{errorMessage:errorMessage});
});
}
}
},
templateUrl: '/static/app/partials/dashboard.html',
controller: 'dashboardCtrl',
})
// parent state: dashboard
.state('dashboard.splash', {
templateUrl: '/static/app/partials/dashboard.splash.html',
controller: 'dashboard.splashCtrl'
})
// parent state: dashboard
.state('dashboard.podSelector', {
// params array can be used instead of url to populate $stateParams
params: ['productIndex', 'commodityIndex'],
templateUrl: '/static/app/partials/dashboard.pod-selector.html',
controller: 'dashboard.podSelectorCtrl'
用户将被重定向到哪个子状态取决于他/她的会话 cookie,我很好奇在哪里进行重定向的合适位置。
我能想到的一个选项是将父仪表板状态设置为非抽象状态以便能够导航到,然后从仪表板父解析承诺中检查会话,然后执行 $state.go to一个孩子取决于结果。理想情况下,我希望将仪表板父状态抽象为它自己无用,但如果它总是会重定向到子状态,那么我想它是否不重要。
我想到的替代方法是使用 $urlRouterProvider.when() 来处理它,类似于:
$urlRouterProvider.when('/dashboard', function($state, sessionService){
if (sessionService.getSession() === undefined) {
$state.go('dashboard.splash');
}
});
这看起来更干净,更合乎逻辑,但是如果我从登录状态转换到仪表板状态,如果我在解析中重定向,我可以使用 $state.go 从登录页面转换('dashboard') 而如果我使用 $urlRouterProvider 我需要做一个 $location.path('/dashboard'),我不知道有多少转换到具有位置路径的状态而不是 $state.go 是不赞成的。
任何建议将不胜感激。
【问题讨论】:
标签: javascript angularjs angular-ui-router