微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

使用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可以使用…这个解决方案可以提供帮助

原文地址:https://www.jb51.cc/regex/356845.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐