Angular UI-Router $urlRouterProvider.when 处理程序结果被 $state 忽略 [英] Angular UI-Router $urlRouterProvider.when handler result ignored by $state
问题描述
我添加了一个 when()
方法来设置/验证路由前缀,如下所示:
I added a when()
method to set/validate a route prefix, which looks something like this:
$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
).
The re-writing happens as I expect, and I get /en-us/home
(GET /home
).
我的状态设置如下:
$stateProvider
.state('base', {
abstract: true,
url: '/{locale}'
})
.state('base.home', {
url: '/home',
views: {
'home@': {
templateUrl: '/partials/home.html'
}
}
});
问题是,使用 when()
s 后,状态不再匹配(视图未加载);即使我手动转到 /en-us/home
,状态仍然没有被触发.
The problem is, with the when()
s in place, the states no-longer match (the view does not get loaded); even when I manually go to /en-us/home
, the state still does not get triggered.
我需要做一些特别的事情来让 $state 重新评估吗?
Do I need to do something special to get $state to re-evaluate?
推荐答案
这将是调整后的 .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/');
}
]);
这一切都与:
小引用:
handler
作为函数
如果处理程序是一个函数,则它是可注入的.如果 $location 匹配,它就会被调用.您可以选择将匹配对象注入为 $match
If 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
处理程序可以返回:
- falsy 表示该规则根本不匹配,然后 $urlRouter 将继续尝试找到另一个匹配的规则.
- a String,它被视为重定向并传递给 $location.url()
- 没有或任何真实值告诉 $urlRouter 网址已被处理
- 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
在此处
这篇关于Angular UI-Router $urlRouterProvider.when 处理程序结果被 $state 忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!