我的状态配置定义如下:
$stateProvider
.state('parentState', {
abstract: true,
url: '/:tenantId/',
param: {
tenantId: {
array: false
}
},
views: {
'header@': {
templateUrl: 'home/header.html',
controller: 'Header as vm'
},
'footer@': {
templateUrl: 'home/footer.html',
controller: 'FooterCtrl as vm'
}
},
resolve: userResolve,
data: {
private: true
}
})
//...4-5 other child states, then a state to handle unknown urls
.state('parentState.otherwise', {
views: {
'@': {
templateUrl: 'home/404/pageNotFound.html',
controller: 'PageNotFoundCtrl as vm'
}
}
});
$urlRouterProvider.otherwise(function ($injector) {
$injector.get('$state').go('parentState.otherwise', {}, {
location: false
});
});
现在,当输入无效网址时,parentState.otherwise
状态正确加载,parentState
参数(即tenantId)也正确填充。
但是,在具有相同无效URL的页面重新加载(刷新,Ctrl + R)上,parentState.otherwise
状态加载,但问题是parentState
参数,即tenantId将作为空字符串(“”)。
答案 0 :(得分:0)
不知何故,对于location: false
,父状态(在这种情况下为parentState
)在页面刷新时没有从URL中获取tenantId
参数。因此,如果我们明确地将tenantId
param传递给子状态,即在打开它时parentState.otherwise
,那么即使在页面刷新时,一切都能正常运行:
$urlRouterProvider.otherwise(function ($injector) {
$injector.get('$state').go('parentState.otherwise', {
tenantId: 'someTenantId'
}, {
location: false
});
});