加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 匹配URL与AngularJS ui-router中的数组列表

发布时间:2020-12-14 06:23:58 所属栏目:百科 来源:网络整理
导读:使用AngularJS ui-router,我需要将一个url与单词列表匹配: 但是,使用正则表达式我尝试使用一个或两个单词,它的工作原理 url: '/{source:(?:John|Paul)}/:id' 但我需要将我的网址与单词列表相匹配. 例: var sourceList = [‘John’,’Paul’,’George’,’R
使用AngularJS ui-router,我需要将一个url与单词列表匹配:

但是,使用正则表达式我尝试使用一个或两个单词,它的工作原理

url: '/{source:(?:John|Paul)}/:id'

但我需要将我的网址与单词列表相匹配.

例:
var sourceList = [‘John’,’Paul’,’George’,’Ringo’];
url:’/ {source :(?:sourceList)} /:id’

有没有办法比较我的网址与匹配的数组列表?

不完全确定你究竟在搜索什么…因为它可能是动态网址匹配器或只是智能正则表达式.这是一个 plunker with working example.它实际上1)只从通过的名单列表构建正则表达式… 2)显示urlMatcher在行动

这里的答案是:使用UrlMatcher.

$urlMatcherFactory and UrlMatchers

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中的例子:

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'
    });

example显示了一个更复杂的解决方案.如果我们在cofnig阶段确实有名单列表……简单连接应该有效.但如果稍后(.run())通过$http可以使用…这个解决方案可以提供帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读