我添加了一个 when()
方法来设置/验证路由前缀,它看起来像这样:
$urlRouterProvider.when(/[a-z]{2}-[a-z]{2}/, [
'$match','myValidatorSrv',
function($match, myValidatorSrv) {
if ( myValidatorSrv.validate($match[0]) ) return true;
// …
return $match.input.replace($match[0],'en-us');
}
]);
$urlRouterProvider.when(/(?![a-z]{2}-[a-z]{2})/, [
'$match','myValidatorSrv',
function($match, myValidatorSrv) {
// …
return $match.input.replace('/', '/en-us/');
}
]);
重写如我所料发生,我得到 /en-us/home
(GET /home
)。
我的状态是这样设置的:
$stateProvider
.state('base', {
abstract: true,
url: '/{locale}'
})
.state('base.home', {
url: '/home',
views: {
'home@': {
templateUrl: '/partials/home.html'
}
}
});
问题是,有了 when()
,状态不再匹配( View 未加载);即使我手动转到 /en-us/home
,状态仍然不会被触发。
我需要做一些特别的事情来让 $state 重新评估吗?
最佳答案
这将是调整后的 .when()
$urlRouterProvider.when(/[a-z]{2}-[a-z]{2}/,
['$match', function($match) {
var supported = ['cs-cz', 'en-us', 'en-gb'];
var isSupported = supported.indexOf($match[0]) >= 0 ;
if(isSupported){
return false;
}
return $match.input.replace($match[0], 'en-us');
}
]);
$urlRouterProvider.when(/^(.(?![a-z]{2}-[a-z]{2}))/,
['$match', function($match)
{
return $match.input.replace('/', '/en-us/');
}
]);
这一切都与:
$urlRouterProvider - when()
for redirection
小引用:
handler
as FunctionIf the handler is a function, it is injectable. It gets invoked if $location matches. You have the option of inject the match object as $match
The handler can return:
- falsy to indicate that the rule didn't match after all, then $urlRouter will continue trying to find another one that matches.
- a String, which is treated as a redirect and passed to $location.url()
- nothing or any truthy value tells $urlRouter that the url was handled
查一下here
关于javascript - Angular UI-Router $urlRouterProvider.when 处理程序结果被 $state 忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172461/