如何在角度 ui-router 状态下添加 guid 正则表达式 [英] How to add guid regex in angular ui-router state

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

问题描述

我有这条路线

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

id 参数应该是一个类似2a542f61-5fff-4607-845d-972e6c3ad1e4"的guid.我如何在网址url: '/{id:? what should I put here}'"中添加这个.

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

这是关于 plunker 的展示

Here is a showcase on plunker with this solution

然后在url路由表达式中指定这个类型:

Then specify this type in url route expression:

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

您可以在阅读更多内容文档中的那个页面

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

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