匹配的URL与AngularJS UI路由器的话数组列表 [英] Matching url with array list of words in AngularJS ui-router
问题描述
使用AngularJS + UI的路由器,我需要一个url与单词列表匹配:
Using AngularJS + ui-router, I need to match a url with list of words:
然而,使用我一直在使用一个或2个字尝试了正则表达式和它的工作
However, using a regex I have tried using one or 2 words and it worked
url: '/{source:(?:John|Paul)}/:id'
但我需要我的网址文字的列表相匹配。
But i need to match my url with an list of words.
例如:
VAR SOURCELIST = ['约翰','保','乔治','林戈'];
网址:'/ {来源:( ?: SOURCELIST)} /:身份证'
example: var sourceList= ['John', 'Paul', 'George', 'Ringo']; url: '/{source:(?:sourceList)}/:id'
有没有办法我的网址比较匹配数组列表?
is there way to compare my url with matching list of array??
推荐答案
没有完全确定你正在寻找什么......因为它可能是一个动态的URL匹配或者仅仅是智能正则表达式。这里是工作的例子一个 plunker。它实际上是1)只有建立从名称列表通过正则表达式... 2)所示 urlMatcher
行动
Not fully sure what exactly you are searching for... because it could be a dynamic url matcher or just smart regex. Here is a plunker with working example. It in fact 1) only builds the regex from the passed list of names... 2) shows urlMatcher
in action
在这里的答案是:使用 UrlMatcher
。
The answer here would be: use the UrlMatcher
.
定义URL模式和参数占位符的语法。这家工厂服务由$ urlRouterProvider幕后缓存,而不必重新解析上的每个位置更改URL模式编译UrlMatcher对象。大多数用户将不需要使用$ urlMatcherFactory直接,但它可能是有用的手艺UrlMatcher对象并将其传递作为URL的状态配置。
Defines the syntax for url patterns and parameter placeholders. This factory service is used behind the scenes by $urlRouterProvider to cache compiled UrlMatcher objects, instead of having to re-parse url patterns on every location change. Most users will not need to use $urlMatcherFactory directly, however it could be useful to craft a UrlMatcher object and pass it as the url to the state config.
例如:
var urlMatcher = $urlMatcherFactory.compile("/home/:id?param1");
$stateProvider.state('myState', {
url: urlMatcher
});
在plunker的例子:
The example in plunker:
var sourceList= ['John', 'Paul', 'George', 'Ringo']
var names = sourceList.join('|');
var urlMatcher = $urlMatcherFactory.compile("/{source:(?:" + names + ")}/:id");
$stateProviderRef
.state('names', {
url: urlMatcher,
templateUrl: 'tpl.root.html',
controller: 'MyCtrl'
});
借助例如呈现出复杂一点的解决方案。如果我们确实有在cofnig阶段名称的列表...简单的联接应该工作。但是,如果稍后将availabel( .RUN()
)通过 $ HTTP
...这个解决方案可以帮助
The example is showing a bit more complex solution. If we do have the list of names during cofnig phase... the simple join should work. But if it will be availabel later (.run()
) via $http
... this solution could help
这篇关于匹配的URL与AngularJS UI路由器的话数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!