由国家$忽略角UI的路由器$ urlRouterProvider.when处理结果 [英] Angular UI-Router $urlRouterProvider.when handler result ignored by $state
问题描述
我添加了一个时()
方法来设置/验证路径preFIX,它看起来是这样的:
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 /家居
(GET /家居
)。
我的状态设置像这样:
$stateProvider
.state('base', {
abstract: true,
url: '/{locale}'
})
.state('base.home', {
url: '/home',
views: {
'home@': {
templateUrl: '/partials/home.html'
}
}
});
问题是,用时()
S IN的地方,美国没有再匹配(视图中没有加载);甚至当我手动去 / EN-US /家居
,国家仍然不被触发。
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.
我需要做一些特别的东西来获得$国家重新评估?
Do I need to do something special to get $state to re-evaluate?
推荐答案
这将是调整。当()
$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/');
}
]);
这一切都关系到:
小举:
处理程序
的功能
如果该处理程序是一个函数,它是可注射的。如果$位置匹配它被调用。你必须注入匹配对象$匹配的选项
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将继续试图找到另外一个匹配。
- 在字符串,它被视为重定向并传递到$ location.url()
- 什么或任何 truthy 值告诉$ urlRouter该URL被处理
- 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
检查一下这里
这篇关于由国家$忽略角UI的路由器$ urlRouterProvider.when处理结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!