svu*_*ala 7 regex angularjs angular-ui-router
使用AngularJS + ui-router,我需要将一个url与单词列表匹配:
但是,使用正则表达式我尝试使用一个或两个单词,它工作
url: '/{source:(?:John|Paul)}/:id'
Run Code Online (Sandbox Code Playgroud)
但我需要将我的网址与单词列表相匹配.
例如:var sourceList = ['John','Paul','George','Ringo']; url:'/ {source :(?:sourceList)} /:id'
有没有办法比较我的网址与匹配的数组列表?
Rad*_*ler 17
不完全确定你究竟在搜索什么...因为它可能是一个动态网址匹配器或只是智能正则表达式.这是一个带有工作示例的plunker.它实际上1)仅从传递的名称列表构建正则表达式... 2)显示urlMatcher在行动中
这里的答案是:使用UrlMatcher.
定义url模式和参数占位符的语法.$ urlRouterProvider在幕后使用此工厂服务来缓存已编译的UrlMatcher对象,而不必在每个位置更改时重新解析url模式.大多数用户不需要直接使用$ urlMatcherFactory,但是创建UrlMatcher对象并将其作为url传递给状态配置可能很有用.
例:
var urlMatcher = $urlMatcherFactory.compile("/home/:id?param1");
$stateProvider.state('myState', {
url: urlMatcher
});
Run Code Online (Sandbox Code Playgroud)
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'
});
Run Code Online (Sandbox Code Playgroud)
该示例显示了更复杂的解决方案.如果我们在cofnig阶段确实有名单列表......简单连接应该有效.但是如果稍后(.run())通过$http......可以提供这种解决方案可以提供帮助
| 归档时间: |
|
| 查看次数: |
9088 次 |
| 最近记录: |