How to add guid regex in angular ui-router state

后端 未结 3 1525
庸人自扰
庸人自扰 2021-01-03 15:49

I have this route



        
3条回答
  •  青春惊慌失措
    2021-01-03 16:15

    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

提交回复
热议问题