如何添加GUID正则表达式的角度UI路由器状态 [英] How to add guid regex in angular ui-router state

查看:172
本文介绍了如何添加GUID正则表达式的角度UI路由器状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这条路线

\r
\r

.STATE('mystate',{\r
    网址:'/ {ID}\r
    templateUrl:/views/partial.html\r
});

\r

\r
\r

的ID参数应该在这样的2a542f61-5fff-4607-845d-972e6c3ad1e4形式一个GUID。
我如何在URL中添加此URL:/ {ID:?我应该怎么把这里}'。


解决方案

您可以定义自己的类型参数。

  VAR GUID_REGEXP = / ^ [AF \\ D] {8}  - ([AF \\ D] {4}  - ){3} [AF \\ D] {12} $ /一世;
$ urlMatcherFactoryProvider.type('的GUID',{
  EN code:angular.identity,
  德code:angular.identity,
  是:函数(项目){
     返回GUID_REGEXP.test(项目);
  }
});

下面是一个展示上plunker这种解决方案

然后指定这种类型的URL路径前pression:

  .STATE('mystate',{
  网址:'/ {GUID的id?}',
  templateUrl:/views/partial.html
});

您可以了解更多关于在文档的页面

I have this route

.state('mystate', {
    url: '/{id}',
    templateUrl: '/views/partial.html'
});

The id param should be a guid in form like this "2a542f61-5fff-4607-845d-972e6c3ad1e4". How do i add this in the url "url: '/{id:?what should i put here}'".

解决方案

You can define your own type of parameter.

var GUID_REGEXP = /^[a-f\d]{8}-([a-f\d]{4}-){3}[a-f\d]{12}$/i;
$urlMatcherFactoryProvider.type('guid', {
  encode: angular.identity,
  decode: angular.identity,
  is: function(item) {
     return GUID_REGEXP.test(item);
  }
});

Here is a showcase on plunker with this solution

Then specify this type in url route expression:

.state('mystate', {
  url: '/{id?guid}',
  templateUrl: '/views/partial.html'
});

You can read more on that page in docs

这篇关于如何添加GUID正则表达式的角度UI路由器状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆